-
-
Save amrza/04658c71ac02d82580855f89b9b3dff4 to your computer and use it in GitHub Desktop.
# Tested on Python 3.6.1 | |
# install: pip install --upgrade arabic-reshaper | |
import arabic_reshaper | |
# install: pip install python-bidi | |
from bidi.algorithm import get_display | |
# install: pip install Pillow | |
from PIL import ImageFont | |
from PIL import Image | |
from PIL import ImageDraw | |
# use a good font! | |
fontFile = "/Users/amirreza/pil/Sahel.ttf" | |
# this was a 400x400 jpg file | |
imageFile = "/Users/amirreza/pil/input.jpg" | |
# load the font and image | |
font = ImageFont.truetype(fontFile, 18) | |
image = Image.open(imageFile) | |
# first you must prepare your text (you dont need this step for english text) | |
text = "سلام ایران" | |
reshaped_text = arabic_reshaper.reshape(text) # correct its shape | |
bidi_text = get_display(reshaped_text) # correct its direction | |
# start drawing on image | |
draw = ImageDraw.Draw(image) | |
draw.text((0, 0), bidi_text, (255,255,255), font=font) | |
draw = ImageDraw.Draw(image) | |
# save it | |
image.save("output.png") |
Thanks.
very thanks!
Thank you so much, this really works.
Works like a charm:)
thanks man
so there's a problem with displaying persian letters in my plot here that they seem to break, i solved the problem of left-to-right with adding [::-1] that someone mentioned in a forum but the letters are now in the right alignment (right-to1-left) yet they're separated, how can i fix them???? i've uploaded a picture of how the problem occurs
p.s. all the other entries here pointed at single senteces to be fixed by arabic_reshaper or some other libraries but ive used data from cells above in jupyternotebook. please help me!!
very thanks!
It help me out :) thanks a milion.
Very kind of you sir.
May God bless you.
Very Coool
so cool. its working most of times. but i have some issues. i want to draw a text ("ذوب اهن") but when im giving the text to 'arabic_reshaper' using reshape methode its cutting off the ("ذوب ") and draws only ("اهن"). can anyone help with this?
u saved ours of my life dude XD
although still we don't have many ttf options... any way to let us use farsi specified fonts like B-zar or B-Nazanin?
hi.
I get this error? have any idea ?
TypeError: '>' not supported between instances of 'str' and 'int'
you can see this too :
issue
Works like a charm! Thank you.
Thank you so much Brother.
Very Cooool
Tanks
A useful and readable piece of code! Thanks for sharing.