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….


This content originally appeared on DEV Community and was authored by Babak

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

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

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"

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

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.


This content originally appeared on DEV Community and was authored by Babak


Print Share Comment Cite Upload Translate Updates
APA

Babak | Sciencx (2025-09-12T16:11:04+00:00) Farsi Image generator. Retrieved from https://www.scien.cx/2025/09/12/farsi-image-generator/

MLA
" » Farsi Image generator." Babak | Sciencx - Friday September 12, 2025, https://www.scien.cx/2025/09/12/farsi-image-generator/
HARVARD
Babak | Sciencx Friday September 12, 2025 » Farsi Image generator., viewed ,<https://www.scien.cx/2025/09/12/farsi-image-generator/>
VANCOUVER
Babak | Sciencx - » Farsi Image generator. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/09/12/farsi-image-generator/
CHICAGO
" » Farsi Image generator." Babak | Sciencx - Accessed . https://www.scien.cx/2025/09/12/farsi-image-generator/
IEEE
" » Farsi Image generator." Babak | Sciencx [Online]. Available: https://www.scien.cx/2025/09/12/farsi-image-generator/. [Accessed: ]
rf:citation
» Farsi Image generator | Babak | Sciencx | https://www.scien.cx/2025/09/12/farsi-image-generator/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.