Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageDraw support for Bangla language #3593

Closed
menon92 opened this issue Jan 18, 2019 · 10 comments
Closed

ImageDraw support for Bangla language #3593

menon92 opened this issue Jan 18, 2019 · 10 comments

Comments

@menon92
Copy link

menon92 commented Jan 18, 2019

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
@radarhere
Copy link
Member

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?

@menon92
Copy link
Author

menon92 commented Jan 18, 2019

@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

@radarhere
Copy link
Member

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.

@radarhere
Copy link
Member

Excellent.

@rakeshvar
Copy link

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()

@nazmul284
Copy link

@menon92 Did you find any solution for this problem? I am facing the same problem. If find any solution please help me. Thank you.

@roundspecs
Copy link

how do i fix it for windows?

@radarhere
Copy link
Member

Regarding the question from @nazmul284, @menon92 liked this comment, so I presume that worked for them.

@zarif25 I suggest installing raqm. https://stackoverflow.com/questions/62939101/how-to-install-pre-built-pillow-wheel-with-libraqm-dlls-on-windows might interest you.

@MahtabShaan
Copy link

I followed this comment and could solve the problem in Ubuntu eventually.

Step 1: !sudo apt-get install libfreetype6-dev libharfbuzz-dev libfribidi-dev gtk-doc-tools
Step 2: !wget -O raqm-0.7.0.tar.gz https://raw.githubusercontent.com/python-pillow/pillow-depends/master/raqm-0.7.0.tar.gz
Now the raqm-0.7.0.tar.gz file should be in your downloads section.
Step 3: !tar -xzvf raqm-0.7.0.tar.gz
Step 4: !cd raqm-0.7.0
Step 5: !./configure --prefix=/usr && make -j4 && sudo make -j4 install
Step 6: Now you just have to reinstall the Pillow library. Activate the correct environment. Then run the following commands:
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow

That's it! Now you have a working Pillow library that can produce proper Bengali and other Indic fonts in the image.

@debdip
Copy link

debdip commented Jun 30, 2023

I found a workaround for windows 10 (64 bit).

As followed this answer, I put both libraqm.dll and fribidi-0.dll (found in x64 folder and downloaded from pillow_website ) in python.exe folder
and added to the PATH variable.

After that it worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants