python - 使用 Python 中的 ImageDraw 在图像中写入文本。如何确定哪种字体适合哪种语言和脚本?

标签 python csv

我试图弄清楚如何根据我拥有的文本选择在 ImageFont.truetype 中使用的字体。

所以我有一个脚本,它读取 CSV 文件并根据其中一个字段中的文本为每一行制作图像。每行还包含一个指定语言和脚本的字段。

这是到目前为止的 python 脚本。它实际上是用于在视频开头添加带有白色文本的黑色图像的脚本的一部分。但我把那部分去掉了,以便于阅读。

import os
import csv
import time

from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw

img_height = 848
img_width = 480
bg_color = (0,0,0)
font_color = (255,255,255)
txt_height_org = 35

with open('simplified.csv', 'r', encoding='utf-8') as f:
    for i, line in enumerate(f):

        if i == 0: continue
        txt_to_write, Transliteration, Translation, Language = line.strip().split(',')

        img = Image.new('RGB', (img_width, img_height), bg_color)
        draw = ImageDraw.Draw(img)
        font = ImageFont.truetype("trado.ttf", txt_height_org)
        txt_width, txt_height = draw.textsize(txt_to_write + "\n" + Translation, font)

        draw.text((img_width/2 - txt_width/2, img_height - txt_height),txt_to_write + "\n" + Translation,font_color,font=font)
        img_fname = 'text' + str(i) + '.png'
        print(txt_to_write)
        img.save(img_fname)

该脚本现在设置为使用 trado.ttf 来表示传统的阿拉伯文本,但即使这样也不起作用。文本从左到右显示且未连接。

这是我正在测试的 CSV 文件。以 UNICODE 格式保存

Text,Transliteration,Translation,Language
你问我爱你有多深,Nǐ wèn wǒ ài nǐ yǒu duō shēn,You ask me how deep you love you,Chinese
我爱你有几分,wǒ ài nǐ yǒu jǐ fēn,I love you a bit,Chinese
لا يهمني بعد الآن,la yohimoni ba3d al2an,I don't care anymore,Arabic

这是 CSV 文件中最后一行的图像

enter image description here

我的 google foo 失败了。没什么,我什至不知道在哪里下载这些 ttf 字体文件。我只是不知所措和卡住了。任何帮助表示赞赏。

谢谢。

最佳答案

看来我必须尝试每种语言的每种字体。反复试验,我找不到引用。我必须尝试每种语言的字体。对于阿拉伯语,我必须做一些不同的事情。这是我使用的。

http://mpcabd.xyz/python-arabic-text-reshaper/

import arabic_reshaper
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw

from bidi.algorithm import get_display

#...

img_height = 848
img_width = 480
bg_color = (0,0,0)
font_color = (255,255,255)
txt_height_org = 35


with open('simplified.csv', 'r', encoding='utf-8') as f:
    for i, line in enumerate(f):

    if i == 0: continue
    txt_to_write, Transliteration, Translation, Language = line.strip().split(',')

    img = Image.new('RGB', (img_width, img_height), bg_color)
    draw = ImageDraw.Draw(img)
    font = ImageFont.truetype(""trado.ttf"", txt_height_org)
    txt_width, txt_height = draw.textsize(txt_to_write, font)
    reshaped_text = arabic_reshaper.reshape(txt_to_write)
    bidi_text = get_display(reshaped_text)
    draw.text((img_width/2 - txt_width/2, img_height - txt_height), bidi_text, font_color,font=font)
    #draw.text((img_width/2 - txt_width/2, img_height - txt_height),txt_to_write + ""\n"" + Translation,font_color,font=font)
    img_fname = 'text' + str(i) + '.png'
    print(txt_to_write)
    img.save(img_fname)

关于python - 使用 Python 中的 ImageDraw 在图像中写入文本。如何确定哪种字体适合哪种语言和脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46394308/

相关文章:

python - 计算字符串中的连续字符

php - 使用 PHP 查询 mysql,放置空字段,并将数据导出为 CSV

csv - 如何使用 isql Firebird 3.04 格式化 CSV 文件中的输出?

string - 为什么 jq --raw-output 参数无法从 @csv 输出中删除引号?

python - 将行添加到现有文件中

python - 如何在 Seaborn 按 YearMonth 订购 X 轴

python-3.x -\ufeff 在 Visualstudio 上写入和读取 csv 文件

postgresql - 当双引号内有\n(换行符)时,如何使用 pgloader 或 PostgreSQL 副本解析 CSV?

iphone - Python APNs 不处理请求

python - 重新加载模块是否会更改先前导入/重新加载的模块中的名称?