Pillow: Bug in rendering Indic fonts

Created on 28 Jan 2015  ·  16Comments  ·  Source: python-pillow/Pillow

ImageFont, ImageDraw do not render complex scripts with ligatures correctly. (On Ubuntu 14.04, Python 3.4.0)

This is how it looks in editor gedit:
gedit
This is how Pillow does it.
pillow

#!/usr/bin/env python3
#-*- coding:utf8 -*-

from PIL import Image, ImageFont, ImageDraw

im = Image.new("RGB",(160, 160))
draw = ImageDraw.Draw(im)

font_telugu = ImageFont.truetype("/usr/share/fonts/truetype/ttf-indic-fonts-core/Pothana2000.ttf",50)
text_telugu = "నిత్య"

font_hindi = ImageFont.truetype("/usr/share/fonts/truetype/ttf-indic-fonts-core/gargi.ttf",50)
text_hindi = "नित्य"

draw.text((10, 10), text_telugu, font=font_telugu)
draw.text((10, 90), text_hindi, font=font_hindi)

im.show()
Bug

Most helpful comment

The following worked for me on Ubuntu 18.04 with Python3.6.7:

  1. sudo pip3 install Pillow -> installs 5.4.1
  2. sudo apt-get install libfreetype6-dev libharfbuzz-dev libfribidi-dev gtk-doc-tools
  3. Install libraqm as described here (using configure, make, make install)
  4. sudo ldconfig This step was needed!
  5. Run the following test script: (Make sure the fonts are installed sudo apt install fonts-indic)
from PIL import Image, ImageFont, ImageDraw

im = Image.new("RGB",(160, 160))
draw = ImageDraw.Draw(im)

font_telugu = ImageFont.truetype("/usr/share/fonts/truetype/fonts-telu-extra/Pothana2000.ttf",50)
text_telugu = "నిత్య"

font_hindi = ImageFont.truetype("/usr/share/fonts/truetype/Gargi/Gargi.ttf",50)
text_hindi = "नित्य"

draw.text((10, 10), text_telugu, font=font_telugu)
draw.text((10, 90), text_hindi, font=font_hindi)
im.show()

All 16 comments

From a quick run through google, it appears that freetype doesn't support the necessary features to correctly combine glyphs into ligatures for complicated fonts/scripts. There are libraries such as harfbuzz [http://www.freedesktop.org/wiki/Software/HarfBuzz/] that build on freetype (+other dependencies) to render those scripts properly.

I have both freetype and harfbuzz up to date on my system.

I'm going to guess that we have zero harfbuzz integration at present.

Closing, we'd require harfbuzz for this. We'd welcome pull requests for the integration.

This should be fixed in 4.2.0 with the merge of #2576

Thanks, @ChillarAnand can you please test this?

@wiredfool Thanks for the patch. This doesn't seem to fix yet.

pip show pillow    
Name: Pillow
Version: 4.2.0
Summary: Python Imaging Library (Fork)
Home-page: https://python-pillow.org
Author: Alex Clark (Fork Author)
Author-email: [email protected]
License: Standard PIL License
Location: /home/chillar/.virtualenvs/py35/lib/python3.5/site-packages
Requires: olefile

Generated image
p

Can you check if raqm is in the features list?

from PIL import features
print (features.check('raqm'))

@rakeshvar @wiredfool Its working. Here is the rendered image.

f

@ChillarAnand How did you get it to work? My features.check('raqm') is showing False.

Here is the script I have used to install it. I guess there is some issue with Pothana fonts itself. Its working with Vani and other fonts.

@ChillarAnand,
I have the same problem for Bangla language like this
image

How can I solve this

@menon92 Can you try with any other fonts?

@ChillarAnand
I have got the solution https://github.com/python-pillow/Pillow/issues/3593

The following worked for me on Ubuntu 18.04 with Python3.6.7:

  1. sudo pip3 install Pillow -> installs 5.4.1
  2. sudo apt-get install libfreetype6-dev libharfbuzz-dev libfribidi-dev gtk-doc-tools
  3. Install libraqm as described here (using configure, make, make install)
  4. sudo ldconfig This step was needed!
  5. Run the following test script: (Make sure the fonts are installed sudo apt install fonts-indic)
from PIL import Image, ImageFont, ImageDraw

im = Image.new("RGB",(160, 160))
draw = ImageDraw.Draw(im)

font_telugu = ImageFont.truetype("/usr/share/fonts/truetype/fonts-telu-extra/Pothana2000.ttf",50)
text_telugu = "నిత్య"

font_hindi = ImageFont.truetype("/usr/share/fonts/truetype/Gargi/Gargi.ttf",50)
text_hindi = "नित्य"

draw.text((10, 10), text_telugu, font=font_telugu)
draw.text((10, 90), text_hindi, font=font_hindi)
im.show()

Ubuntu 18.04 has libraqm0, so you should be able to install it and skip building Raqm manually.

Was this page helpful?
0 / 5 - 0 ratings