Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
942 views
in Technique[技术] by (71.8m points)

unicode - Drawing multilingual text using PIL

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...