naim5am
naim5am

Reputation: 1374

Writing text in images with ImageDraw in Python. How to figure out which font for which Language and script?

I'm trying to figure out how to choose what font to use in ImageFont.truetype depending on the text I have.

So I have this script that reads a CSV file and makes an image for each row based on the text in one of the fields. Each row also contains a field specifying language and script.

This is the python script so far. It's actually part of a script used to add a black image with white text at the beginning of videos. But I took out that part to make it easier to read.

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)

The script is now set to use trado.ttf for traditional Arabic text, and even that is not even working. The text is showing up from left to right and not joined.

here's the CSV file I'm testing with. Saved in 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

Here's the image the for the last line in the CSV file

enter image description here

My google foo is failing me. Nothing, I can't even figure out where to download these ttf font files. I'm just overwhelmed and stuck. Any help appreciated.

Thanks.

Upvotes: 1

Views: 599

Answers (1)

naim5am
naim5am

Reputation: 1374

Seems I'll have to try each font for each language. Trial and error, I couldn't find a reference. I'll have to try a font for each language. For Arabic, I had to do a little something different. Here's what I used.

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)

Upvotes: 1

Related Questions