Last active
December 16, 2017 08:23
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using Pillow, right-to-left letters get segmented on an image and not very useful. Here's a work-around for it.