Skip to content

Instantly share code, notes, and snippets.

@arastu
Created May 4, 2021 13:51
Show Gist options
  • Save arastu/1ca517ded27deee894feb76d6e96a240 to your computer and use it in GitHub Desktop.
Save arastu/1ca517ded27deee894feb76d6e96a240 to your computer and use it in GitHub Desktop.
Write rtl (Persian, Arabic, Hebrew) text on images using python and pillow
# First of all you need install *libraqm* on your machin.
# The Raqm library encapsulates the logic for complex text layouts and provides a convenient API.
# libraqm relies on the following libraries: FreeType, HarfBuzz, FriBiDi,
# make sure that you install them before installing libraqm if not available as package in your system.
# if you using macos you can install libraqm with homebrew
# $> brew install libraqm
# Pillow wheels since version 8.2.0 include a modified version of libraqm
# that loads libfribidi at runtime if it is installed.
# The .text function takes the direction argument to the
# libraqm library, which enables pillow to support bidirectional text (using FriBiDi),
# shaping (using HarfBuzz), and also proper script itemization.
import os
from PIL import Image, ImageDraw, ImageFont
def main():
image = Image.open(os.path.join(os.path.dirname(__file__), "nature.jpeg"))
font = ImageFont.truetype(
os.path.join(
os.path.dirname(__file__), "vazir-font-v28.0.0", "Vazir-Regular.ttf"
),
100,
)
text = "طبیعت زیبای دوست داشتنی"
canvas = ImageDraw.Draw(image)
canvas.text((0, 0), text, (255, 255, 255), font=font, direction="rtl")
image.save(os.path.join(os.path.dirname(__file__), "result.jpg"))
if __name__ == '__main__':
main()
@arastu
Copy link
Author

arastu commented May 4, 2021

nature

@arastu
Copy link
Author

arastu commented May 4, 2021

result

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