5

我在Python中使用PIL编写了一些代码,它将UTF-8字符打印到图像上。将双向文本打印到图像

我注意到,对于加盟的双向脚本(如阿拉伯语),同样的代码无法字符正确连接(最初的形式仅是选择,中间和最终的形式不被利用)

谁能推荐一个方法或解决这个问题的技巧?

+0

我刚刚有了一个快速浏览一下他们的网站,并我的猜测是图书馆没有能力正确处理BIDI。如果他们这样做,他们当然不会做广告。 – 2012-12-10 05:53:48

+0

是[this](http://pypi.python.org/pypi/python-bidi/)的任何帮助? – 2012-12-10 05:55:37

回答

0

我做了以下事情:Python + Wand(Python Lib)+ arabic_reshaper(Python Lib)+ bidi.algorithme(Python Lib)。这同样适用于PIL /枕头,您需要使用arabic_reshaperbidi.algorithm并通过生成的文本到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) 

Arabic typography in images