How to output unicode characters and lines into an image file?

Question:

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)
Asked By: alvas

||

Answers:

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)
Answered By: beerbajay
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.