Pillow: ImageDraw support for Bangla language

Created on 18 Jan 2019  ·  5Comments  ·  Source: python-pillow/Pillow

I am trying to convert Bangla text to Image using PIL ImageDraw method. To do this is use following code,

from PIL import ImageFont, Image, ImageDraw, ImageChops, ImageOps

w, h = 64, 64
w0, h0 = 256, 256
blank = Image.new('L', (w0 * 5, h0 * 3), 255)
font = '/path/to/font/Siyamrupali.ttf'
font = ImageFont.truetype(font, 20)  
char = 'দৃষ্টিভঙ্গি'
img = Image.new("L", (w0 * 5, h0 * 3), 255) 
draw = ImageDraw.Draw(img)
draw.text((w0, h0), char, font=font)
diff = ImageChops.difference(img, blank)
lx, ly, hx, hy = diff.getbbox()
img = img.crop((lx, ly, hx, hy))
img.save('image' + '.png')

It should convert text দৃষ্টিভঙ্গি to .png file. It does

But the image is broken like below
image

My OS, Python and Pillow versions

  • OS: Ubuntu 14.04 LTS
  • Python: 3.6.3
  • Pillow: 4.2.1
Font Rendering Linux

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 5 comments

If I mention that complex text rendering can be used through setting the ImageFont.truetype layout_engine argument to raqm, is that new and helpful information?

@radarhere
I replace line
font = ImageFont.truetype(font, 20)
by
font = ImageFont.truetype(font=font, size=20, layout_engine=ImageFont.LAYOUT_RAQM)

still It generate same broken image

Okay. Loading up a VM instance of Ubuntu 14.04, I tried your script, and it didn't work.
I installed libharfbuzz-dev and libfribidi-dev, re-installed Pillow, and it didn't work.
I then ran depends/install_raqm.sh, re-installed Pillow, and without requiring the layout_engine=ImageFont.LAYOUT_RAQM option, it worked.

So I expect if you try some combination of that, you should find a solution.

Excellent.

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()
Was this page helpful?
0 / 5 - 0 ratings