我在 Python 中有一些使用 PIL 的代码,可以将 UTF-8 字符打印到图像上。
我注意到,对于加入像阿拉伯语这样的 Bidi 脚本,相同的代码无法正确连接字符(仅选择初始形式,未使用中间形式和最终形式)
任何人都可以推荐一种方法或技术来解决这个特定问题吗?
我在 Python 中有一些使用 PIL 的代码,可以将 UTF-8 字符打印到图像上。
我注意到,对于加入像阿拉伯语这样的 Bidi 脚本,相同的代码无法正确连接字符(仅选择初始形式,未使用中间形式和最终形式)
任何人都可以推荐一种方法或技术来解决这个特定问题吗?
如果您想继续使用 PIL,请使用pyarabicshaping
withpybidi
或者您可能需要考虑切换到用于文本整形的pangocairo
哪些用途。HarfBuzz
What I did is the following: 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
and bidi.algorithm
and pass the generated text to draw.text((10, 25), artext, font=font)
:
from wand.image import Image as wImage
from wand.display import display as wdiplay
from wand.drawing import Drawing
from wand.color import Color
import arabic_reshaper
from bidi.algorithm import get_display
reshaped_text = arabic_reshaper.reshape(u'لغةٌ عربيّة')
artext = get_display(reshaped_text)
fonts = ['C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\DroidNaskh-Bold.ttf',
'C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\Thabit.ttf',
'C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\Thabit-Bold-Oblique.ttf',
'C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\Thabit-Bold.ttf',
'C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\Thabit-Oblique.ttf',
'C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\majalla.ttf',
'C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\majallab.ttf',
]
draw = Drawing()
img = wImage(width=1200,height=(len(fonts)+2)*60,background=Color('#ffffff'))
#draw.fill_color(Color('#000000'))
draw.text_alignment = 'right';
draw.text_antialias = True
draw.text_encoding = 'utf-8'
#draw.text_interline_spacing = 1
#draw.text_interword_spacing = 15.0
draw.text_kerning = 0.0
for i in range(len(fonts)):
font = fonts[i]
draw.font = font
draw.font_size = 40
draw.text(img.width / 2, 40+(i*60),artext)
print draw.get_font_metrics(img,artext)
draw(img)
draw.text(img.width / 2, 40+((i+1)*60),u'ناصر test')
draw(img)
img.save(filename='C:\\PATH\\OUTPUT\\arabictest.png'.format(r))
wdiplay(img)