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

Font rendering issue with ttc font #7871

Closed
camprevail opened this issue Mar 12, 2024 · 13 comments
Closed

Font rendering issue with ttc font #7871

camprevail opened this issue Mar 12, 2024 · 13 comments

Comments

@camprevail
Copy link

camprevail commented Mar 12, 2024

What did you do?

It seems that font rendering changed for the worse between 7.2.0 and 8.x, not sure which version exactly. (edit, it changed between 7.2 and 8.0). I had this issue a few years ago when I tried to upgrade to 8.x so I had to revert, but now I really have a need to upgrade.
Below is a comparison image, with the caption text rendered with draw.text() in Pillow 7.2.0 on the left and 10.2 on the right. On the right, you can see that the text appears to have lost it's softer anti-aliased look and kerning in some spots - some letters are touching each other.
I searched up and down and couldn't find any other reports of anything like this, I did try a few suggestions like setting draw.textmode = 'L' and enabling the kern feature, but nothing worked.

All I'm looking for is a solution or suggestions to get the old look back. The font used is msgothic.ttc, index 1.

diffs

What are your OS, Python and Pillow versions?

  • OS: Windows 10
  • Python: 3.10.11
  • Pillow: 10.2
base = Image.open('assets/misc/bg_new.png').convert('RGB')
draw = ImageDraw.Draw(base)
draw.fontmode = 'L'
caption_font = ImageFont.truetype("assets/font/msgothic.ttc", size=12, index=1)
text = "Warrior with a fan. She fights with the force of\nthe wind. She has a very strong personality,..."
draw.text((171, 780), text, (164, 144, 64), font=caption_font)
base.show()
@camprevail
Copy link
Author

camprevail commented Mar 12, 2024

Small update, tried changing the layout engine for this font object to layout_engine=ImageFont.Layout.BASIC. That seems to kind of fixed the kerning issue, but the antialias setting with draw.fontmode = 'L' still does nothing, no difference from setting it to '1'.
I don't think antialias will fix the overall look of it being completely different... it almost doesn't look like the same font?
test2

@camprevail
Copy link
Author

Tried switching to arial and it looks much better. Perhaps this is a problem with the msgothic TTC, or TTC font rendering specifically?
test3

@camprevail camprevail changed the title Font rendering changed after 7.2.0, looks worse now. Font rendering changed after 7.2.0, not rendering TTC font correctly. Mar 12, 2024
@radarhere radarhere changed the title Font rendering changed after 7.2.0, not rendering TTC font correctly. Font rendering changed after 7.2.0, not rendering TTC font correctly Mar 12, 2024
@radarhere
Copy link
Member

It seems like you're uploading screenshots of the images, rather than the images themselves. Moving forward, I think it would be more helpful to have the images themselves.

I'm guessing that you're installing Pillow from the wheels, using something like python3 -m pip install Pillow?

font rendering changed for the worse between 7.2.0 and 8.x, not sure which version exactly.

If you could figure out exactly which version, that would be interesting.

@camprevail
Copy link
Author

camprevail commented Mar 12, 2024

I uploaded screenshots because the rest of the image is much larger and irrelevant, I really only needed to show the affected area, but if I do more tests I guess I'll just try to make a smaller canvas with text only that can be uploaded here.

And yeah I installed via pip.

@radarhere
Copy link
Member

I really only needed to show the affected area

You could also just crop the original image? I think the screenshots are introducing an element of blurriness.

@camprevail
Copy link
Author

I'm pretty confident they aren't, do you not see the difference between the two images? It looks pretty clear to me, even on my phone

@radarhere
Copy link
Member

I do see a difference. I was just trying to improve the quality of what we were looking at here - I don't think it's worth discussing further.

@camprevail camprevail changed the title Font rendering changed after 7.2.0, not rendering TTC font correctly Font rendering issue with ttc font in 10.1.0 onwards Mar 12, 2024
@camprevail camprevail changed the title Font rendering issue with ttc font in 10.1.0 onwards Font rendering issue with ttc font Mar 12, 2024
@camprevail
Copy link
Author

Sorry, forgot to add the font arg to draw.text. Looks like it changed between 7.2 and 8.0. Here's the comparison images
pillowtest-7 2 0
pillowtest-8 0
My code is:

from PIL import Image, ImageDraw, ImageFont

font = ImageFont.truetype('msgothic.ttc', size=12, index=1)
base = Image.new('RGB', (250, 50), color=(255, 255, 255))
draw = ImageDraw.Draw(base)
mytext = 'The quick brown fox jumps over...'

draw.text((5, 10), mytext, (0,0,0), font=font)
base.show()

@camprevail
Copy link
Author

camprevail commented Mar 12, 2024

Here's a much better test revealing that something is happening between 22 and 23pt sizes.
pillowtest-7 2 0
pillowtest-8 0 0

@nulano
Copy link
Contributor

nulano commented Mar 12, 2024

Based on the images, this is almost certainly caused by #4955 fixing #891. The previous behaviour was caused by a bug where Pillow was ignoring embedded bitmaps in a font.

OpenType fonts can contain embedded bitmaps in the EBDT table. This is supposed to provide better quality when rendering at small font sizes. Unfortunately, it seems that the font you are using did not implement this very well. You may be able to remove the embedded bitmap table with a tool such as FontForge.

@camprevail
Copy link
Author

Alright thanks for the suggestion, that worked and was very easy.
pillowtest-10 2 0

@radarhere
Copy link
Member

Thanks very much nulano.

Is this resolved then?

@camprevail
Copy link
Author

Yeah I suppose so. I'll close it

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

3 participants