Reputation: 21
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.
Upvotes: 2
Views: 1281
Reputation: 41
It seems like this question can benefit from a consolidated, concise solution for silicon-chip Mac users.
Don't use the Apple-provided Python interpreter.
Reinstall and overwrite your Python interpreter with Homebrew. Then use Homebrew to install the Pillow and its dependencies, pip install Pillow, and you should be good to go.
brew uninstall python
brew install python
brew install freetype fribidi harfbuzz
brew install libraqm
brew install pillow
pip install pillow
That's it.
Upvotes: 0
Reputation: 21
I had the same issue and was able to resolve it by uninstalling Pillow
pip uninstall Pillow
Then reinstalling as follows
pip install --upgrade Pillow --global-option="build_ext" --global-option="--enable-raqm"
There is some more information on the build options here.
This assumes you have already installed libraqm with homebrew
brew install libraqm
I am unsure if this is necessary but you may also need these dependencies as mentioned by Mark Setchell
brew install freetype harfbuzz fribidi
Upvotes: 2
Reputation: 207678
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
Upvotes: 0
Reputation: 1
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:
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).
Upvotes: 0