Skip to content

Instantly share code, notes, and snippets.

@mostafabahri
Last active December 16, 2017 08:23
Show Gist options
  • Save mostafabahri/ee3be0dcccc3c12f7a6b07f3de8bb9c9 to your computer and use it in GitHub Desktop.
Save mostafabahri/ee3be0dcccc3c12f7a6b07f3de8bb9c9 to your computer and use it in GitHub Desktop.
Snippet for writing Persian or other RTL langs on image with Python PIL
# -*- coding: utf-8 -*-
#!/usr/bin/env python3
import PIL
from PIL import ImageFont, Image, ImageDraw
from bidi.algorithm import get_display
import arabic_reshaper
def rtl_fix(text):
# some magic happens here and makes RTL drawing fixed
reshaped_name = arabic_reshaper.reshape(text)
return get_display(reshaped_name)
daniel = "دانیال"
fixed_name = rtl_fix(daniel)
x_y_pos = (1, 2)
color = (0, 0, 0) # black
yekan_font = ImageFont.truetype("./Yekan.ttf", 80)
draw = ImageDraw.Draw(Image.open("./sample.jpg"))
draw.text(fixed_name, x_y_pos, color, font=yekan_font)
img.show()
@mostafabahri
Copy link
Author

mostafabahri commented Mar 9, 2017

Using Pillow, right-to-left letters get segmented on an image and not very useful. Here's a work-around for it.

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