Skip to content

Instantly share code, notes, and snippets.

Last active July 10, 2023 08:22
  • Star 77 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
How to write RTL(Arabic/Persian) text on images in python.
# 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 =
# 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"output.png")
Copy link

maysam commented Apr 14, 2018


Copy link

cool :"D

Copy link

You just saved my life :) thanks

Copy link

thanks amigo

Copy link

Awesome! Thanks

Copy link

really helpful

Copy link

how can I write Persian or Arabic numbers??

Copy link


Copy link

A useful and readable piece of code! Thanks for sharing.

Copy link


Copy link

very thanks!

Copy link

Thank you so much, this really works.

Copy link

Works like a charm:)

Copy link

thanks man

Copy link

Can anyone tell me, if it's possible to align all the letters? I mean, i need to write 3 arabic letters and they are just looks like this
and i need them to be like this
I've tried too many fonts... But I don't know how to do this.

Copy link

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!!

Copy link

very thanks!

Copy link

It help me out :) thanks a milion.

Copy link

Very kind of you sir.
May God bless you.

Copy link

Very Coool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment