Multilingual Text-Over-Image Rendering Pipeline: Python, Celery, S3 & Canvas
Introduction
There is a need to easily put multilanguage text on a source image. The system will output multiple images. One image for one language.
For example, if we have this image:
We can draw a text on top of it like this:
After that, we will automatically have images in Chinese, French, Japanese, Korean, Portuguese, etc:
The source image that is containing no text is uploaded to AWS S3.
The text-display-definitions, such as position, color, font, size, etc are stored in a PostgreSQL database table.
The source text is uploaded to Transifex. On Transifex, a provided machine translator will translate the source text to multiple languages. Human editor can fix the translation later.
Once translation is finished, Transifex will notify our app via a webhook URL. A celery task will download the source image from S3, load text display definition from PostgreSQL, and load translations from Transifex, and combine those data to generate translate images. The translated images will then be uploaded to S3.
A client app is accessing the translated image using an S3 URL similar to the original image, but it has
a locale code added to it. For example, if the original image name is melati.png,
the Korean version name is melati.ko.png.
Image Translation Document
An image translation document is entities relationship marker. It marks the relating source image, source texts, and the display definitions of the source texts.
A user modifies image translation document via a form.
Defining Source Texts
Multiple texts can be put on an images. An input field is dynamically added when the user wants to add a new text.
Once the form is saved, the source texts are sent to Transifex to be translated.
Defining Text Display
We are using FabricJS, a great HTML5 canvas library, to develop UI that allows users to define text display. On this UI, a user can draw a rectangle to mark the position of the text. This UI also provides a form, so the user can choose a font family, font-weight, color, etc.
Once the user click save, the display definitions will be stored in the database.
Translated Images Generation
Once Transifex accesses our webhook endpoint, a celery task to generate translated images will be run.
The celery task downloads all the needed information from Transifex, PostgreSQL database, and S3. Those information is passed as parameters to a PhantomJS script that uses FabricJS to generate the translated images.
Once the translated images are generated, they will be uploaded back to S3.
The Sequence Diagram
Modern Production Architecture (2026): Replacing PhantomJS with Headless Chromium / Pillow + HarfBuzz
In modern cloud deployments on Google Cloud Run, GKE, and AWS ECS, legacy PhantomJS workers are replaced by either pooled Playwright Headless Chromium contexts (for full CSS/Fabric.js HTML5 Canvas fidelity) or native Pillow + Raqm (HarfBuzz + FriBidi) pipelines for sub-40ms CJK (Chinese, Japanese, Korean) and bidirectional Arabic/Hebrew shaping. Authored by Senior Cloud Engineer, GCP Professional Cloud Architect, and Backend Engineer Aldian Fazrihady:
# media_worker/localized_banner.py
import io
from PIL import Image, ImageDraw, ImageFont
def render_localized_overlay(
source_png_bytes: bytes,
translated_text: str,
font_path: str,
box: tuple[int, int, int, int],
fill_rgba: tuple[int, int, int, int] = (15, 23, 42, 255),
rtl: bool = False,
) -> bytes:
base = Image.open(io.BytesIO(source_png_bytes)).convert("RGBA")
draw = ImageDraw.Draw(base)
x1, y1, x2, y2 = box
max_width = x2 - x1
# Auto-fit font size so German/Indonesian/Portuguese expansions never clip
font_size = 48
font = ImageFont.truetype(font_path, font_size, layout_engine=ImageFont.Layout.RAQM)
while font_size > 16 and draw.textlength(translated_text, font=font) > max_width:
font_size -= 2
font = ImageFont.truetype(font_path, font_size, layout_engine=ImageFont.Layout.RAQM)
anchor_x = x2 if rtl else x1
draw.text(
(anchor_x, y1),
translated_text,
font=font,
fill=fill_rgba,
anchor="ra" if rtl else "la",
direction="rtl" if rtl else "ltr",
)
out = io.BytesIO()
base.save(out, format="WEBP", quality=88, method=6)
return out.getvalue()
Frequently Asked Questions — Multilingual Image Generation Pipelines
How do you prevent CJK and Arabic/RTL text from rendering as tofu boxes in Docker workers?
Install Noto CJK and Noto Sans Arabic font families inside the worker Docker image alongside libraqm, harfbuzz, and fribidi so Pillow's Raqm layout engine (or Headless Chromium) shapes ligatures and right-to-left text runs accurately.
How do you handle text expansion when English strings translate into longer German or Indonesian phrases?
Store bounding-box coordinates in PostgreSQL rather than single anchor points, and dynamically step down font size or wrap lines inside the bounding box before compositing onto the master asset and uploading locale-keyed objects to S3/Cloud Storage + CDN.
Can I hire a Senior Cloud, Backend, or Full Stack Engineer to build an automated media localization pipeline?
Yes. Book a $100 USD / Rp 1,000,000 1-Day Programming Sprint or explore our AI Application Engineer, GCP Professional Data Engineer, and Full Stack Engineer pillar guides.