使用PIL绘制多语言文本 [英] Drawing multilingual text using PIL

查看:202
本文介绍了使用PIL绘制多语言文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用PIL绘制多语言文本。比方说,我想画的文字 - ひらがな - 平假名,히라가나。但是PIL的ImageDraw.text()函数一次只能使用一种字体,所以我不能正确绘制这些文本,因为它需要英文,日文和韩文字体。



<到目前为止,我没有找到一个简单的解决方案,像传递多个字体到PIL,以便它可以为每个Unicode字符选择适当的字体(像现代SDK或Web浏览器一样)。

我在想什么,我应该遍历每个字符,并确定自己使用每个字符的字体。但是我不禁想到,要做到这一点,一定要有一个更简单的方法。



我的方向是否正确?如果有更好的解决方案,可以使用其他语言或其他图像库。


是不是有一个更简单的方法?



解决方案

你只需要选择一个Unicode字体。例如:

$ p $ import Image
导入ImageFont,ImageDraw $ b $ image = Image.new(RGB,
font =ImageFont.truetype(/ Library / Fonts / Arial Unicode.ttf)
draw = ImageDraw.Draw(image)
a = uひらがな - Hiragana,히라가나 ,14)
draw.text((50,50),a,font = font)
image.save(a.png)
pre>


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.

解决方案

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")

这篇关于使用PIL绘制多语言文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆