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

0

5

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

Hardandfast answered 21/12, 2016 at 20:46 Comment(6)
Is the problem persistent for this niqqud only? or does it show similar problem for other niqqud as well?Tetrahedron
When I run the same code I get an error at this line fnt = ImageFont.truetype(f[1], 40) stating IOError: cannot open resource. Did you face this error?Tetrahedron
Also did you try this for other letters or are you facing this problem only for the letter 'lamed'?Tetrahedron
@JeruLuke This happens not just with the lamed, but with the vav (last letter) as well. You might be receiving that error because the Tammey FrankCLM font is not standard. It's available online.Hardandfast
Have you tried it out with any another image processing library like OpenCV or skimage?Tetrahedron
It looks like the font author's fault. If the bug is consistent in other apps you can report the bug to the author; If it's not a bug, you can try to write a special Nikud positioning for particular letters, but it's a lot of work.Milquetoast

© 2022 - 2024 — McMap. All rights reserved.