18

I'm having trouble drawing multilingual text using PIL. Let's say I want to draw text - "ひらがな - Hiragana, 히라가나". But PIL's ImageDraw.text() function takes only one font at a time, so I cannot draw this text correctly, because it requires English, Japanese, and Korean fonts all together.

So far, I had no luck finding a simple solution like passing multiple fonts to PIL, so that it can choose appropriate font for each Unicode character (Like modern SDK or web browsers do).

What I'm thinking is, I should iterate over each character, and determine which font to use for each character by myself. But I can't help thinking that there must be an easier way to do this.

Am I going in the right direction? Isn't there an easier way?

PS) It's OK to use another language or another imaging library if there's a much better solution.

dda
  • 6,030
  • 2
  • 25
  • 34
redism
  • 500
  • 7
  • 18

1 Answers1

23

You just need to pick a Unicode font. Example:

import Image
import ImageFont, ImageDraw
image=Image.new("RGB",[320,320])
draw = ImageDraw.Draw(image)
a=u"ひらがな - Hiragana, 히라가나"
font=ImageFont.truetype("/Library/Fonts/Arial Unicode.ttf",14)
draw.text((50, 50), a, font=font)
image.save("a.png")

Outputs this

dda
  • 6,030
  • 2
  • 25
  • 34
  • 2
    Yes, I just figured out that there are fonts which can cover many unicode glyphs. But what I really wanted to do was applying different fonts for different languages. (Best chosen font for each language) For now, I'm using [ttfquery](http://ttfquery.sourceforge.net/) to check if each unicode's glyph is contained in a certain font or not. Thanks for your answer. – redism Jul 13 '12 at 07:28
  • You could probably speed up things -- if you have a lot of text to draw -- by extracting language families from your strings and querying one character for each language family. – dda Jul 13 '12 at 08:37
  • 1
    @dda Your example is exactly what I was looking for: there are even single font sets that cover multiple languages. But this one you've shown doesn't seem to come with most Linux distributions. Do you know of other fonts that have this much support? – john_science Jul 09 '15 at 17:09
  • 1
    I would suggest installing the Code 2000 font. https://en.wikipedia.org/wiki/Code2000 – dda Jul 09 '15 at 22:30
  • 1
    This worked for me straightaway in python-2.7 (anaconda). `+n!` – uhoh Nov 29 '17 at 04:22
  • 2
    @uhoh happy to see a five year old answer still to be of help! ☺ – dda Nov 29 '17 at 04:25
  • I've just asked [Drawing multilingual text using PIL **and saving as 1-bit and 8-bit bitmaps**](https://stackoverflow.com/q/47545359/3904031) – uhoh Nov 29 '17 at 04:59