如何使用PIL(枕头)来绘制任何语言的文字? [英] How to use PIL (pillow) to draw text in any language?

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

问题描述

我用Python PIL在背景图像上渲染用户输入文本(我正在使用枕头)。



代码很简单:

$ p $ draw = ImageDraw.Draw( im)
draw.text((x,y),text,font = font,fill = font_color)

问题是,用户可能会输入任何语言,我怎么能确定使用哪个 font


$ b $ ps:我知道我必须先有字体文件,所以我搜索并找到了 Google Noto ,下载所有的字体,把它们放在 / usr / local / share / fonts / 中,但是这些字体是用语言分隔的,所以我仍然无法加载这是一种可以呈现所有用户输入文本的字体。

解决方案

NoTo(字面上只是Adobe的 Source Pro 字体,因为Google更容易以这种方式进行销售)isn'一个字体,这是一个字体家族。当您去下载时,Google会明确告诉您有很多不同的版本不同的目标语言,原因有两个:


  1. 如果你需要排版整个星球的一套已知的脚本,字形比单字体更适合(OpenType字体由于字形ID被编码为USHORT字段,所以每个文件有65535个字形的硬限制,而字体是合成的:字母实际上可以是字母 C 和符号°,所以它依赖于三字形:两个真正的字形和一个虚拟的合成,这样就可以很快地用完空间),即使字体可以适合所有的字形也是一样的,
  2. 脚本可能需要被渲染得相当不同,这取决于它所使用的语言,所以即使是用于中文和日文的单一字体,或者用于阿拉伯语和乌尔都语,根本不起作用。虽然OpenType字体可以通过被告知要使用哪种变体集合以及基于特定语言标签的哪种构图规则来处理,这是在InDesign或LaTeX中很好的控制类型,并且是正在进行的字体最糟糕的事情(比如Android的webview)。

所以正确的解决方法是抓住 > all 字体,然后根据您为其生成文本的 {script,language} 对选择正确的字体。比你想要做的更复杂吗?是。有必要吗?同样是的=)

I'm rendering user input text on a background image with Python PIL(I'm using pillow).

the code is simple:

draw = ImageDraw.Draw(im)
draw.text((x, y), text, font=font, fill=font_color)

the problem is, the user may input in any language, how could I determine which font to use?

ps: I know I have to have font files first, so I searched and found Google Noto, downloaded all the fonts, put them in /usr/local/share/fonts/, but these fonts are separated by language, so I still can't load a font that can render all user input texts.

解决方案

NoTo (which is literally just Adobe's Source Pro fonts with a different name because it's easier for Google to market it that way) isn't a single font, it's a family of fonts. When you go to download them, Google explicitly tells you that there are lots of different versions for lots of different target languages, for the two simple reasons that:

  1. if you need to typeset the entire planet's set of known scripts, there are vastly more glyphs than fit in a single font (OpenType fonts have a hard limit of 65535 glyphs per file due to the fact that glyph IDs are encoded as USHORT fields. And fonts are compositional: the "letter"  can actually be the letter C and the symbol °, so it relies on three glyphs: two real glyphs, and one virtual composition. You run out of space real fast that way) , and
  2. even if a font could fit all the glyphs, the same script may need to be rendered rather different depending on the language it's used for, so even having a single font for both Chinese and Japanese, or for Arabic and Urdu, simply doesn't work. While OpenType fonts can cope with that by being told which variation sets to use, and which compositional rules based on specific language tags, that is the kind of control that works great in InDesign or LaTeX, and is the worst thing for fonts that are going to be used in control-less context (like an Android webview, for instance).

So the proper solution is to grab all the fonts, and then pick the right one based on the {script, language} pair you're generating text for. Is that more complicated than what you're trying to do? Yes. Is it necessary? Equally yes =)

这篇关于如何使用PIL(枕头)来绘制任何语言的文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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