python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.19k stars 2.22k forks source link

Tamil font is rendered in a wrong manner in matplotlib #8481

Open GitPavan123 opened 2 days ago

GitPavan123 commented 2 days ago

I have been working on with a project in matplotlib. I'm trying to display tamil texts in a pie chart. I did downloaded and specified the appropriate font paths (In my case Brahmini, Noto sans tamil, Tiro, etc...) Everything does render the tamil font but it is completely in a wrong manner. Figure(1) is the actual rendering. Figure(2) is the required rendering.

CODING

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

font_path = r"Fonts\Tamil Font.TTF" //Replace with a tamil font path

tamil_font = FontProperties(fname=font_path) 

sizes = [25, 35, 20, 20]  
labels = ['எனக்கு ', 'மின்விசிறி', 'கலை', 'வாழைப்பழம்']  
colors = ['#ff9999','#66b3ff','#99ff99','#ffcc99']

fig, ax = plt.subplots()
wedges, texts, autotexts = ax.pie(sizes, colors=colors, autopct='%1.1f%%', startangle=90)

ax.axis('equal')

for i, autotext in enumerate(autotexts):
    autotext.set_text(labels[i])  
    autotext.set_color("black")  
    autotext.set_fontsize(12)  
    autotext.set_fontproperties(tamil_font)  

for text in texts:
    text.set_fontproperties(tamil_font)

plt.show()

Screenshot 2024-10-18 024111 image

radarhere commented 2 days ago

Hi. Do you have raqm installed?

>>> from PIL import features
>>> features.check("raqm")
True
GitPavan123 commented 1 day ago

No what is it's used for?

radarhere commented 1 day ago

Complex text rendering. It is "recommended for all non-English text". See https://github.com/python-pillow/Pillow/issues/6018#issuecomment-1030893495 for an instance where using Raqm has solved Tamil rendering in the past.

How did you install Pillow?

If you're a Windows user, I suggest looking at https://pillow.readthedocs.io/en/stable/installation/building-from-source.html

On Windows this requires compiling FriBiDi and installing fribidi.dll into a directory listed in the Dynamic-link library search order (Microsoft Learn) (fribidi-0.dll or libfribidi-0.dll are also detected).

GitPavan123 commented 1 day ago

I installed pillow in my conda environment using the command "pip install pillow". But even though i upgrade my pillow version still it returns false for the checking the existence of raqm.

radarhere commented 1 day ago

What operating system are you using?

Could you uninstall Pillow, and then run pip install -v Pillow and show us the output?

GitPavan123 commented 1 day ago

I am using windows os

radarhere commented 23 hours ago

Have you attempted placing fribidi in the correct location? Here is fribidi.dll from our recent Pillow 11.0.0 release for various architectures - fribidi.zip

https://pillow.readthedocs.io/en/stable/installation/building-from-source.html

On Windows this requires compiling FriBiDi and installing fribidi.dll into a directory listed in the Dynamic-link library search order (Microsoft Learn) (fribidi-0.dll or libfribidi-0.dll are also detected).

GitPavan123 commented 2 hours ago

I have installed raqm and now i get true for the following code,

>>> from PIL import features
>>> features.check("raqm")

Now how do i enable or activate raqm to render text in matplotlib pie chart?

radarhere commented 59 minutes ago

Hmm. I would have expected the correct output from matplotlib once you had done that.

If matplotlib is still not working as you expect, does the following Pillow-only code work?

from PIL import Image, ImageDraw, ImageFont

font = ImageFont.truetype(r"Fonts\Tamil Font.TTF", 25)

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

for i, text in enumerate(['எனக்கு ', 'மின்விசிறி', 'கலை', 'வாழைப்பழம்']):
    draw.text((20, 20+i*50), text, font=font, fill="white")
im.show()