PIL draw.text() 将包含阿拉伯语连字的字符串显示为两个单独的字形

6 python string python-imaging-library python-3.x arabic-support

我有python代码。“??” 是一个字符串,由两个字母组成?和 ?. 但它们结合在阿拉伯语中。我将这个词赋予 PIL 库函数。但它分别保存两个字母的图像。我怎样才能把它们结合起来。?

data2= "??"    
draw.text(((W-w)/2,(H-h)/2),data2,(0,0,0),font=font)
draw = ImageDraw.Draw(img)
img.save("abc"+".png")
Run Code Online (Sandbox Code Playgroud)

输出:那是?和 ?.

小智 5

您可以使用 python_arabic_reshaper 库来正确编写文本。这是该库的github 存储库。

安装库:

pip install --upgrade arabic-reshaper
Run Code Online (Sandbox Code Playgroud)

然后像这样导入:

import arabic_reshaper
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样使用它:

text_to_be_reshaped = '????? ??????? ?????'
reshaped_text = arabic_reshaper.reshape(text_to_be_reshaped)
Run Code Online (Sandbox Code Playgroud)