使用PIL(Python成像库)使用变音符号(“nikud”,发声标记) [英] Writing text with diacritic ("nikud", vocalization marks) using PIL (Python Imaging Library)

查看:322
本文介绍了使用PIL(Python成像库)使用变音符号(“nikud”,发声标记)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PIL在图片上写简单的文本很简单。

  draw = ImageDraw.Draw(img)
draw.text((10,y),text2,font = font,fill = forecolor)

,当我试图写希伯来语标点符号(称为nikud或ניקוד),字符不重叠,因为他们应该。 (我想这个问题也和阿拉伯语和其他类似的语言有关。)

在支持的环境中,这两个词占据相同的空间/宽度(下面的例子取决于你的系统,因此图像):

סֶפֶרספר



然而,当用PIL I获得:


$ b $ p

因为库可能不符合字距(?)规则。






是否可以让字符和希伯来语标点符号占用相同的空格/宽度而不需要手动书写字符位置? $ b

图片 - nikud和字母间距http://tinypic.com/r/jglhc5/5



图片网址: http: http://tinypic.com/r/jglhc5/5

解决方案

滑稽,5年后,来自@Nasser Al-Wo haibi,我意识到如何做:

需要用BIDI算法翻转文本。



<$ p $来自bidi.algorithm的编码:utf-8 - * -
import get_display
导入PIL.Image,PIL.ImageFont,PIL.ImageDraw
img = PIL.Image.new(L,(400,200))
draw = PIL.ImageDraw.Draw(img)
font = PIL.ImageFont.truetype(rc:\ ($ 10,10),'BiDi:'+ t1,fill = 255'之前的'$'
t1 =''
t1 = ,font = font)

t2 = get_display(t1)#< ---这里是魔术< ---
draw.text((10,50),'BiDi :'+ t2,fill = 220,font = font)

img.save('bidi-test.png')

@纳赛尔的答案具有额外的价值,可能仅与阿拉伯文本有关(阿拉伯文字母的变化形状和连贯性基于他们的邻接字母,在希伯来语中所有字母都是分开的),所以只有双方的部分是相关的这个问题

在示例结果中,
的第二行是正确的形式,并且正确的发声标记定位。




谢谢@tzot的帮助+代码片段

a-propos:

不同字体行为的样本用希伯来语nikud。不是所有的字体都是相同的:


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

解决方案

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.

# -*- coding: utf-8 -*-
from bidi.algorithm import get_display
import PIL.Image, PIL.ImageFont, PIL.ImageDraw
img= PIL.Image.new("L", (400, 200))
draw = PIL.ImageDraw.Draw(img)
font = PIL.ImageFont.truetype( r"c:\windows\fonts\arial.ttf", 30)
t1 = u'סֶפֶר ספר!'
draw.text( (10,10), 'before BiDi :' + t1, fill=255, font=font)

t2 = get_display(t1)        # <--- here's the magic <---
draw.text( (10,50), 'after BiDi: ' + t2, fill=220, font=font)

img.save( 'bidi-test.png')

@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:

这篇关于使用PIL(Python成像库)使用变音符号(“nikud”,发声标记)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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