← Blog · · 2 min read

Pillow: Devanagari breaks silently when libraqm can't find fribidi on macOS

On this page (5 sections)
  1. What happened
  2. Reproduce it
  3. Why
  4. The fix
  5. Takeaways

With Pillow 12.0.0's PyPI wheel on macOS, कि rendered with क at +0px instead of +26px, so the vowel sign sat after the consonant, and the default call raised no warning. Pillow had silently fallen back to BASIC layout because libraqm couldn't load fribidi.

TL;DR Refuse to render Indic text unless features.check("raqm") is True. On macOS with Pillow 12.0.0's PyPI wheel and Homebrew's fribidi installed, make fribidi loadable (DYLD_FALLBACK_LIBRARY_PATH=/opt/homebrew/lib).

What happened

The ffmpeg in our video pipeline has no drawtext filter, so we draw all on-screen text with Pillow and lay it over the video as PNG overlays. Our glyph check reads the font's cmap (its table of supported characters) with fontTools, and it passed.

Reproduce it

Python 3.13.5, Pillow 12.0.0, fontTools 4.67.0, macOS 27.0.1 on arm64, with Kohinoor Devanagari at 96px. The wheel bundles libharfbuzz.0.dylib but not fribidi.

$ .venv/bin/python repro.py; echo exit=$?
raqm available: False | raqm None | fribidi None | harfbuzz None
cmap has every codepoint of 'कि' : True ['U+0915', 'U+093F']
default layout_engine chosen: BASIC | warnings: []
requested RAQM  -> used BASIC क found at +  0px: matra AFTER क (wrong) | warning: Raqm layout was requested, but Raqm is not available. Falling back to basic layout.
Run Engine requested Engine used क offset Result
fribidi not found default BASIC 0px wrong, no warning
fribidi not found RAQM BASIC 0px wrong, with a warning
DYLD_FALLBACK_LIBRARY_PATH=/opt/homebrew/lib default RAQM 26px correct
DYLD_FALLBACK_LIBRARY_PATH=/opt/homebrew/lib BASIC BASIC 0px wrong

Why

Complex layout in Pillow goes through libraqm. Raqm needs fribidi, and the wheel doesn't ship it. Homebrew's copy sits in /opt/homebrew/lib; setting DYLD_FALLBACK_LIBRARY_PATH=/opt/homebrew/lib made it load, so we infer dlopen was not looking there by default.

Without raqm, Pillow picks BASIC, which draws characters in the order they are stored. The i-matra ि (U+093F) is stored after क but must be drawn to its left; under BASIC it lands on the right. A cmap check can't catch this: every codepoint is in the font, and only the layout is wrong.

The fix

We set the library path and added a guard (from repro.py):

if not features.check("raqm"):
    print("GUARD: FAIL - raqm not available; refusing to render Indic text with Layout.BASIC")
    sys.exit(2)
print("GUARD: OK - raqm", features.version("raqm"))

Without the path the guard printed GUARD: FAIL and exited 2. With DYLD_FALLBACK_LIBRARY_PATH=/opt/homebrew/lib it printed GUARD: OK - raqm 0.10.3 and exited 0.

Takeaways

  1. Treat features.check("raqm") == False as a hard error for Indic, Arabic or other complex scripts.
  2. A font-coverage check proves the glyphs exist, not that they are laid out correctly; also look at a rendered sample.
  3. Never pass layout_engine=ImageFont.Layout.BASIC for Devanagari, even when raqm is available.

Applies to: Pillow 12.0.0 (PyPI wheel in a venv, run directly as .venv/bin/python) on Python 3.13.5, macOS 27.0.1 arm64, with Homebrew fribidi 1.0.16 and Kohinoor Devanagari; conda base's Pillow 12.0.0 also reported raqm unavailable, but we did not check its cause or the fix there. We did not test Linux, Windows, other Pillow versions or scripts other than Devanagari.

Does your rendering stack check for a shaping engine at startup?

How this was made: a short note drafted by an AI agent (Claude) from our own engineering logs; numbers are checked against those logs before publishing.