The niqqud are not aligned properly while drawing text in Hebrew using PIL (Python Imaging Library)

390 views Asked by At

I'm using Pillow / PIL to draw hebrew letters with nikud. I noticed that the nikudim (plural for nikud) are not properly aligned and sometimes overlap other letters.

Any suggested fix for this? I've tried a few fonts, and they all seem to have their own issues.

Here's the code that I'm using.

from bidi.algorithm import get_display
from PIL import Image, ImageDraw, ImageFont

fonts = [
    ('Tammey FranckCLM', '/PATH/TO/FONT/TaameyFrankCLM-Medium.ttf'),
    ('Times New Roman', '/PATH/TO/FONT/Times New Roman.ttf'),
    ('Arial', '/PATH/TO/FONT/Arial.ttf')
]

im = Image.new(mode='RGBA', size = (1000, 1000), color = (0, 0, 0, 255))
draw = ImageDraw.Draw(im)

height = 100
for f in fonts:
    fnt = ImageFont.truetype(f[1], 40)
    text = 'עָלֵינוּ'
    text_bidi = get_display(text, base_dir='R')
    draw.text((100, height), f[0], font=fnt, fill=(255, 255, 255))
    draw.text((500, height), text_bidi, font=fnt, fill=(255, 255, 255))
    height += 70
im.show()
im.close()

Note, that due to text direction, the text renders in the proper direction on SO, but not in the terminal. In the terminal it looks like below. I'm using the BiDi algorithm to reverse the word.

enter image description here

Here's an example of the output. You can see the nikud (i.e. the dots underneath and next to the letters) are not the same in every font. The fonts render properly in text editors, but not in PIL.

Any suggestions would be appreciated. Thanks!

enter image description here

For reference, it should look like below.

enter image description here

0

There are 0 answers