DEV Community

Cover image for Farsi Image generator
Babak
Babak

Posted on

Farsi Image generator

During the development of a personal project, I needed to build a script that converts Farsi (Persian) text into an image. To make the process easier — and a bit more fun — I chose the excellent Gradio framework to build a simple interactive interface.

What is Gradio?

Gradio is a popular open-source Python library that makes it easy to create interactive, web-based UIs for machine learning models, APIs, or even plain Python functions. It’s a favorite among data scientists and ML engineers who want to quickly share their work — no HTML, CSS, or JavaScript required.

Gradio also has a new serverless option, Gradio-Lite, which is amazing for quick demos, but unfortunately it’s not suitable for this project.

Installing Gradio

I prefer using uv as my package manager since it’s blazing fast (thanks to Rust under the hood):

uv add gradio
Enter fullscreen mode Exit fullscreen mode

Installing libraqm

Farsi uses a complex script where the shape of each character depends on its position in the word. To render it correctly, we need libraqm, which handles text shaping.

Here’s how to install it:

macOS

Using Homebrew:

brew install libraqm
Enter fullscreen mode Exit fullscreen mode

If you’re using fish shell, you’ll also need to update your PKG_CONFIG_PATH:

set -gx PKG_CONFIG_PATH "(brew --prefix libraqm)/lib/pkgconfig:$PKG_CONFIG_PATH"
Enter fullscreen mode Exit fullscreen mode

Windows

Follow this StackOverflow answer for installation instructions.

Installing Pillow

Once libraqm is set up, install Pillow with:

uv pip install Pillow --no-binary=Pillow
Enter fullscreen mode Exit fullscreen mode

This ensures Pillow is compiled with libraqm support.

Converting Farsi Text to an Image

Now that everything is installed, we can write a simple script that takes:

  • A font file
  • Your Farsi text
  • Image dimensions

Here’s the script:

Run the script, then open your app at:
http://localhost:8007/

Here’s the result 🎉

!! If you find this post helpful or have any thought, please don't be shy and share it here with me.

Top comments (0)