使用 PIL(Python 图像库)编写带有变音符号(“nikud”,发声标记)的文本
使用 PIL 在图像上编写简单的文本很容易。
draw = ImageDraw.Draw(img)
draw.text((10, y), text2, font=font, fill=forecolor )
但是,当我尝试编写希伯来语标点符号(称为“nikud”或נйקוד)时,字符没有按应有的方式重叠。 (我猜这个问题也与阿拉伯语和其他类似语言相关。)
在支持环境中,这两个单词占用相同的空间/宽度(下面的示例取决于您的系统,因此是图像):
סֶפֶר ספר
但是在绘图时使用 PIL 的文本我得到:
ס ֶ פ ֶ ר,
因为该库可能不遵守字距调整(?)规则。
是否可以让字符和希伯来标点符号占用相同的空间/宽度而无需手动编写字符定位?
Writing simple text on an image using PIL is easy.
draw = ImageDraw.Draw(img)
draw.text((10, y), text2, font=font, fill=forecolor )
However, when I try to write Hebrew punctuation marks (called "nikud" or ניקוד), the characters do not overlap as they should. (I would guess this question is relevant also to Arabic and other similar languages.)
On supporting environment, these two words take up the same space/width (the below example depends on your system, hence the image):
סֶפֶר ספר
However when drawing the text with PIL I get:
ס ֶ פ ֶ ר
since the library probably doesn't obey kerning(?) rules.
Is it possible to have the character and Hebrew punctuation mark take up the same space/width without manually writing character positioning?
image - nikud and letter spacing http://tinypic.com/r/jglhc5/5
image url: http://tinypic.com/r/jglhc5/5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
至于阿拉伯语变音符号:Python +Wand(Python Lib) +arabic_reshaper(Python Lib) +bidi.algorithme(Python Lib)。 这同样适用于PIL/Pillow,您需要使用
arabic_reshaper
和bidi.algorithm
并将生成的文本传递给draw。 text((10, 25), arttext, font=font)
:As for Arabic diacritics : Python +Wand(Python Lib) +arabic_reshaper(Python Lib) +bidi.algorithme(Python Lib). The same applies to PIL/Pillow, you need to use the
arabic_reshaper
andbidi.algorithm
and pass the generated text todraw.text((10, 25), artext, font=font)
:有趣的是,5 年后,在 @Nasser Al-Wohaibi 的大力帮助下,我意识到该怎么做:
需要使用 BIDI 算法反转文本。
@Nasser的答案具有额外的价值,可能只与阿拉伯语文本相关(阿拉伯语中的字母根据其相邻字母改变形状和连通性,希伯来语中所有字母都是分开的),所以只有比迪部分与这个问题相关。
在样本结果中,
第二行是正确的形式,以及正确的发声标记定位。
谢谢 @tzot 的帮助 + 代码片段
a-propos:
希伯来语“nikud”的不同字体行为示例”。 并非所有字体的行为都相同:
funny, after 5 years, and with great help fron @Nasser Al-Wohaibi, I realized how to do it:
Reversing the text with a BIDI algorithm was needed.
@Nasser's answer has extra value that's probably relevant only to arabic texts (the letters in arabic change shape and connected-ness based on their neiboring letters, in hebrew all letters are separate), so only the bidi part was relevant for this question.
in the sample result,
the 2nd line is the correct form, and correct vocalization marks positioning.
thank you @tzot for help + code snippets
a-propos:
samples of different font behavior with Hebrew "nikud". Not all fonts behave the same:
您正在使用什么系统? 它适用于我的 Gentoo 系统; 字母的顺序是相反的(我只是从你的问题中复制粘贴),这对我来说似乎是正确的,尽管我对 RTL 语言不太了解。
产生:
编辑:我应该说使用
Deja Vu Sans
字体并不是偶然的; 虽然我不太喜欢它(但我发现它的字形比 Arial 更好),但它具有可读性,它扩展了 Unicode 覆盖范围,而且它似乎比Arial Unicode MS
更适合许多非 MS 应用程序代码>.What system are you working on? It works for me on my Gentoo system; the order of the letters is reversed (I just copy-pasted from your question), which seems correct to me although I don't know much about RTL languages.
produces:
EDIT: I should say that using the
Deja Vu Sans
font was not accidental; although I don't like it much (and yet I find its glyphs better than Arial), it's readable, it has extended Unicode coverage, and it seems to work better with many non-MS applications thanArial Unicode MS
.对于所有语言阿拉伯语、希伯来语、日语甚至英语,您都可以使用此代码:
这将解决所有问题,但不要忘记使用 Unicode 字体,例如 在此处输入链接说明
for all languages Arabic, Hebrew, Japanese even english you can use this code:
this will fix the all problems but don't forget to use Unicode fonts such as enter link description here
在我看来,这个案子很简单。 您可以使用 True Type 字体并使用
以下示例:PIL 的 True type 字体
在这里您可以找到希伯来语 True Type 字体:
希伯来语 true type 字体
祝你好运,或者就像我们用希伯来语说的 - Mazal' Tov。
Looks to me that the case is quite simple. You can use True Type fonts and use
Here's the example:True type fonts for PIL
Here you can find Hebrew True Type fonts:
Hebrew true type fonts
Good luck or like we saying in Hebrew - Mazal' Tov.