Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
701 views
in Technique[技术] by (71.8m points)

unicode - Writing text with diacritic ("nikud", vocalization marks) using PIL (Python Imaging Library)

Writing simple text on an image using PIL is easy.

draw = ImageDraw.Draw(img)
draw.text((10, y), text2, font=font, fill=forecolor )

However, when I try to write Hebrew punctuation marks (called "nikud" or ?????), the characters do not overlap as they should. (I would guess this question is relevant also to Arabic and other similar languages.)

On supporting environment, these two words take up the same space/width (the below example depends on your system, hence the image):

????? ???

However when drawing the text with PIL I get:

? ? ? ? ?

since the library probably doesn't obey kerning(?) rules.

Is it possible to have the character and Hebrew punctuation mark take up the same space/width without manually writing character positioning?

image - nikud and letter spacing http://tinypic.com/r/jglhc5/5

image url: http://tinypic.com/r/jglhc5/5

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As for Arabic diacritics : Python +Wand(Python Lib) +arabic_reshaper(Python Lib) +bidi.algorithme(Python Lib). The same applies to PIL/Pillow, you need to use the arabic_reshaper and bidi.algorithm and pass the generated text to draw.text((10, 25), artext, font=font):

from wand.image import Image as wImage
from wand.display import display as wdiplay
from wand.drawing import Drawing
from wand.color import Color
import arabic_reshaper
from bidi.algorithm import get_display

reshaped_text = arabic_reshaper.reshape(u'???? ??????')
artext = get_display(reshaped_text)

fonts = ['C:\Users\PATH\TO\FONT\Thabit-0.02\DroidNaskh-Bold.ttf',
         'C:\Users\PATH\TO\FONT\Thabit-0.02\Thabit.ttf',
         'C:\Users\PATH\TO\FONT\Thabit-0.02\Thabit-Bold-Oblique.ttf',
         'C:\Users\PATH\TO\FONT\Thabit-0.02\Thabit-Bold.ttf',
         'C:\Users\PATH\TO\FONT\Thabit-0.02\Thabit-Oblique.ttf',
         'C:\Users\PATH\TO\FONT\Thabit-0.02\majalla.ttf',         
         'C:\Users\PATH\TO\FONT\Thabit-0.02\majallab.ttf',

         ]
draw = Drawing()
img =  wImage(width=1200,height=(len(fonts)+2)*60,background=Color('#ffffff')) 
#draw.fill_color(Color('#000000'))
draw.text_alignment = 'right';
draw.text_antialias = True
draw.text_encoding = 'utf-8'
#draw.text_interline_spacing = 1
#draw.text_interword_spacing = 15.0
draw.text_kerning = 0.0
for i in range(len(fonts)):
    font =  fonts[i]
    draw.font = font
    draw.font_size = 40
    draw.text(img.width / 2, 40+(i*60),artext)
    print draw.get_font_metrics(img,artext)
    draw(img)
draw.text(img.width / 2, 40+((i+1)*60),u'???? test')
draw(img)
img.save(filename='C:\PATH\OUTPUT\arabictest.png'.format(r))
wdiplay(img)

Arabic typography in images


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...