Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can we handle such case? #22

Closed
navenduagarwal opened this issue Jun 8, 2018 · 2 comments
Closed

How can we handle such case? #22

navenduagarwal opened this issue Jun 8, 2018 · 2 comments

Comments

@navenduagarwal
Copy link

navenduagarwal commented Jun 8, 2018

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 :(

@navenduagarwal navenduagarwal changed the title How we handle such case How can we handle such case? Jun 8, 2018
@ravarage
Copy link
Contributor

ravarage commented Jun 14, 2018

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
Copy link
Owner

mpcabd commented Jun 21, 2018

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

@mpcabd mpcabd closed this as completed Jun 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants