python - 是否有任何新方法可以在 python 中的图像上编写 RTL(阿拉伯语等)文本
问题描述
我曾经看到很多程序可以很好地呈现阿拉伯文本而没有错误,但是当涉及到 python 时,我只知道一个库(arabic_reshaper)并且它还没有效率,因为它包含错误,所以我想知道是否有有什么方法可以像 api 或一些扭曲的有效方法来完成这项任务?
解决方案
是的。你必须使用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")
推荐阅读
- excel - 根据 excel / VBA 中第二列中的匹配项重复下拉验证条目
- c# - 如何将十进制转换为十六进制和十六进制字符串的异或
- html - 交换字体时的字体转换
- python - 如何通过生命周期配置使用 boto3 更改 S3 存储桶文件夹中所有对象的存储类?
- kubernetes - 无法通过 macvlan 访问不同工作人员(节点)中的 pod
- r - oath_listener() 需要一个交互式环境
- python - 在 python 列表中打印特定项目
- javascript - 将动态类型分配给字符串
- jquery - 悬停时如何为引导卡设置动画?
- service-worker - 如何在用户脚本中使用服务工作者