2

I want to create a jpg or any other image format files for chinese characters, how do i do it?

My input textfile (in utf8) looks like this:

阿贝•斯兰尼\t是\t美国人

Reading it is simple, i could simply do codecs.open('intext.txt','r','utf8).read().strip().split('\t') but how can I output an image file that looks like this: enter image description here

Eventually, the whole jpg might look like this: enter image description here

So the exact questions are:

  1. How do I output connecting lines into an image file using python?
  2. How do I output unicode characters into an image file using python?
  3. How do I output both connecting lines and unicode in a structured order into an image file using python? (i.e. progressively create the first image to create a tree like image like the second image)
Dharman
  • 30,962
  • 25
  • 85
  • 135
alvas
  • 115,346
  • 109
  • 446
  • 738
  • `"in a structured order"` What do you mean by this? `"progressively create the first image to create a tree"` this still doesn't quite make sense; you have to keep track of all the points where things should intersect and draw the lines so they intersect there. Or do you want a ready-made tree class that generates exactly your expected output? (this probably doesn't exist) – beerbajay Nov 27 '13 at 10:20
  • I suppose you could recursively work on smaller sections of the image and use a generic "draw two labelled lines and an arc" method on that subimage. – beerbajay Nov 27 '13 at 10:34

1 Answers1

7

PIL (the Python Imaging Library) can both draw lines and unicode text to images, using the ImageDraw module. For your text to be printed correctly, you'll have to ensure that you've got a unicode string (not utf-8 encoded) and a font which contains the characters you want to display.

To create a new image:

image = Image.new('RGB', (xsize, ysize))

To get a draw instance for that image:

draw = ImageDraw.Draw(image) 

To draw text:

draw.text((xpos,ypos), mytext, font=ImageFont.truetype('myfont.ttf', 11))

To draw a line:

draw.line((startx,starty, endx,endy), fill=128)

To draw an arc:

draw.arc((startx,starty, endx,endy), startangle, endangle)
beerbajay
  • 19,652
  • 6
  • 58
  • 75
  • how do i create a new image with PIL? I've looked high and low for tutorials but all it gives is an image editing stuff not image creation... – alvas Nov 28 '13 at 09:55