mpcabd / python-arabic-reshaper

Reconstruct Arabic sentences to be used in applications that don't support Arabic
MIT License
398 stars 80 forks source link

How can we handle such case? #22

Closed navenduagarwal closed 6 years ago

navenduagarwal commented 6 years ago

original text : علامات المحبة

1

My code is below:

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

def create_text_image(base_image_path=settings.MEDIA_ROOT + '/creative/a1.jpg',
                      output_image_path=settings.MEDIA_ROOT + '/images/output.jpg', text_start_height=820,
                      font_size=40, text_color="#ffffff", text_arabic='', text_english=''):
    # Load & Draw Image
    image = Image.open(base_image_path)
    draw = ImageDraw.Draw(image)
    print('Hello')
    # use a truetype font
    reshaped_text = arabic_reshaper.reshape(text_arabic)
    bidi_text = get_display(reshaped_text)
    font = ImageFont.truetype(settings.MEDIA_ROOT + 'fonts/regular.ttf', font_size)

    # prepare text
    iw, ih = image.size
    w_a, h_a = font.getsize(bidi_text)
    w_e, h_e = font.getsize(text_english)
    textX_a = int((iw - w_a) / 2)
    textX_e = int((iw - w_e) / 2)

    draw.text((textX_a, text_start_height), bidi_text, font=font, fill=text_color)
    draw.text((textX_e, text_start_height+h_a+10), text_english, font=font, fill=text_color)

    image.save(output_image_path)

Thanks in advance. Pillow rtl is not working, so I am stuck :(

ravarage commented 6 years ago

my code as below show up fine , its font related issue use Arial font that tested to be working fine

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

image = Image.new('RGB', (200, 200), color='White') draw = ImageDraw.Draw(image)

use a truetype font

reshaped_text = arabic_reshaper.reshape('علامات المحبة') bidi_text = get_display(reshaped_text) font = ImageFont.truetype(font='arial.ttf', size=30)

draw.text((0, 0), bidi_text,font=font, fill=(0,0,0,0))

image.show()

mpcabd commented 6 years ago

Like @ravarage said it's purely a font issue, use Arial like suggested, or try one of Google's fonts, to my knowledge those fonts have support for all characters, I might be mistaken though, only by testing you'd find out.

See my comment here on issue #20 for a similar case