Home > Software engineering >  Pillow not recognizing Libraqm installation on Mac OS
Pillow not recognizing Libraqm installation on Mac OS

Time:11-30

I need to be able to render text in python in various fonts and using various writing systems that use variable substitution of characters (Arabic, Hindi, Bengali). On a previous machine I had no issue doing this, but I just moved into a new machine with the same conda environment and it doesn't seme to work.

Mac OS 12.6 Python version 3.9.13 Libraqm version 0.9.0 (installed with brew) Pillow version 9.2.0 (installed with pip)

Here's a minimal reproducible example. It should produce the Urdu word لڑكا – with the two groups of two letters attached – but instead it's producing something that looks like ا ک ڑ ل, with each letter disconnected. It's rendering the specific font correctly, so it's not a matter of the font not being found. When I run the last few lines of code checking for the 'raqm' feature, I get False.

from PIL import ImageFont
from PIL import ImageDraw
from PIL import Image

font = ImageFont.truetype('KawkabMono-Bold.ttf',12,layout_engine=ImageFont.Layout.RAQM)

img = Image.new('1', (200, 100), 'white')
d = ImageDraw.Draw(img)
d.text((100,50), 'لڑكا', fill='black', font=font)

img.save('test.png')

from PIL import features
features.check_feature(feature='raqm')

I've tried installing and reinstalling a few times, and building from different sources. My guess is that libraqm is not on the correct path for Pillow, but I'm not sure how to check or fix this.

I tried running the minimal reproducible text and expected to see a properly render, RTL text with the correct rendering of text. Instead, I got an image file of the text rendered left-to-right and disconnected.

CodePudding user response:

On Mac Os 12.6.1 and Python 3.10.8 System Interpreter I was able to get libraqm to work with Pillow. Configuration follows:

  1. Python Interpreter version 10.8 installed with Homebrew.
  2. Libraqm version 0.9.0 installed with Homebrew.
  3. Pillow version 9.3.0 installed with pip.

First, I tried using a python venv, and I got UserWarning: Raqm layout was requested, but Raqm is not available. Falling back to basic layout.

Next, I added the following to the code you posted: print(features.check("raqm")) and it printed False as expected.

I used my Python System Interpreter (3.10.8), installed Pillow and I was able to get libraqm to load with Pillow.

Image without libraqm. Image with libraqm

I added a fonts directory with the font specified on your snippet above. I loaded the font with the following code:

features.check_feature(feature='raqm')
print(features.check("raqm"))
custom_font = os.path.abspath(
    os.path.join(
        os.path.dirname(__file__), 'fonts/KawkabMono-Bold.ttf'
    )
)

If the librqam library is not loaded by the python interpreter, then your ouput will be like you described it ( individual characters).

CodePudding user response:

I think you additionally need fribidi and harfbuzz installed before PIL/Pillow:

brew install fribidi harfbuzz

Note that you can get PIL/Pillow's configuration, build settings, features and supported formats with:

python3 -m PIL
  • Related