首页 > 解决方案 > 是否有任何新方法可以在 python 中的图像上编写 RTL(阿拉伯语等)文本

问题描述

我曾经看到很多程序可以很好地呈现阿拉伯文本而没有错误,但是当涉及到 python 时,我只知道一个库(arabic_reshaper)并且它还没有效率,因为它包含错误,所以我想知道是否有有什么方法可以像 api 或一些扭曲的有效方法来完成这项任务?

标签: pythonpython-imaging-library

解决方案


是的。你必须使用arabic_reshaper & python-bidi 和合适的字体。在尝试了许多字体后,我发现只有 2 种字体有效:“Pak Nastaleeq.ttf”和“AA Sameer Qamri Regular.ttf”您可以下载任何这些字体的 .ttf 文件并使用它。这是python中的示例代码,描述了如何执行此操作:

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import arabic_reshaper
from bidi.algorithm import get_display
text = "چوكسے ہنجاریں الجمل تیج اکساہٹ"
reshaped_p = arabic_reshaper.reshape(text)
text = get_display(reshaped_p)
cat_image = Image.open("test_image.png")
drawing_on_img = ImageDraw.Draw(cat_image)
font = ImageFont.truetype('Pak Nastaleeq.ttf',size=15)
text_color = (201,50,250)
text_coordinates = (0,0)
drawing_on_img.text(text_coordinates,text,font=font,fill='black')
cat_image.save("text_on_test_imager.png")

推荐阅读