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

查看:31
本文介绍了使用 PIL(Python 成像库)使用变音符号(“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 )

但是,当我尝试书写希伯来语标点符号(称为nikud"或 ניקוד)时,字符没有按应有的方式重叠.(我猜这个问题也与阿拉伯语和其他类似语言有关.)

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

סֶפֶר ספר

但是,当我用 PIL 绘制文本时:

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?

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

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

推荐答案

有趣的是,5 年后,在@Nasser Al-Wohaibi 的大力帮助下,我意识到了如何去做:

funny, after 5 years, and with great help fron @Nasser Al-Wohaibi, I realized how to do it:

需要使用 BIDI 算法反转文本.

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:windowsfontsarial.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 的回答具有额外的价值,可能仅与阿拉伯语文本相关(阿拉伯语中的字母会根据其相邻字母改变形状和连接性,在希伯来语中所有字母都是分开的),因此只有 bidi 部分与此相关问题.

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

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

thank you @tzot for help + code snippets

a-propos:

使用希伯来语nikud"的不同字体行为示例.并非所有字体的行为都相同:

samples of different font behavior with Hebrew "nikud". Not all fonts behave the same:

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

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