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

Absent pixels when drawing font #4569

Closed
gaoxinge opened this issue Apr 20, 2020 · 7 comments
Closed

Absent pixels when drawing font #4569

gaoxinge opened this issue Apr 20, 2020 · 7 comments
Labels
Bug Any unexpected behavior, until confirmed feature. Font Rendering

Comments

@gaoxinge
Copy link

gaoxinge commented Apr 20, 2020

What did you do?

  • I use pillow to draw font.

What actually happened?

1

  • I observe there are some absence pixels on the top of numbers: 0, 2, 3, 8, 9 .

What are your OS, Python and Pillow versions?

  • OS: win10
  • Python: 3.6.1
  • Pillow: 7.1.1
from PIL import Image, ImageFont, ImageDraw

ttf_path = "vista-hei-all.ttf"
text = "0123456789"
text_size = 35

font = ImageFont.truetype(ttf_path, text_size)
text_width, text_height = font.getsize(text)
canvas = Image.new("RGBA", [text_width, text_height], (255, 0, 0, 0))
draw = ImageDraw.Draw(canvas)
draw.text((0, 0), text, font=font, fill="#000000")
canvas.show()
@gaoxinge gaoxinge changed the title question about font draw absence pixels when font draw Apr 20, 2020
@radarhere radarhere changed the title absence pixels when font draw Absent pixels when drawing font Apr 20, 2020
@radarhere
Copy link
Member

Are you able to replicate this problem with a font that you can link to?

@gaoxinge
Copy link
Author

@radarhere I place a zip here.

@radarhere radarhere added Font Rendering Bug Any unexpected behavior, until confirmed feature. labels Apr 20, 2020
@radarhere
Copy link
Member

As a note for future investigation of this, here's a clear way to demonstrate that this is a bug - removing the '0' from '0123456789' causes the missing pixels to be drawn without a problem.

@nulano
Copy link
Contributor

nulano commented Apr 21, 2020

I have seen this before in some previous tests. I believe the cause is #185, as the solution there seems to be to count the text distance from the bottom instead of from the top. (Confusingly, this patch made the ascender variable refer to the descender height.) This is wrong and works only because most fonts tend to have a gap at the top. I believe I have fixed this properly in my WIP PR for #4511 (comment), by enabling hinting for FT_GetGlyphCBox in _imagingft.getsize.

@gaoxinge
Copy link
Author

@nulano Nice!

@gaoxinge
Copy link
Author

I have run the code below on pillow version 7.1.1 and 8.0.1 respectively, and the bug seems to be fixed.

from PIL import Image, ImageFont, ImageDraw

ttf_path = "vista-hei-all.ttf"
text = "0123456789"
text_size = 35

font = ImageFont.truetype(ttf_path, text_size)
text_width, text_height = font.getsize(text)
canvas = Image.new("RGBA", [text_width, text_height], (255, 0, 0, 255))
draw = ImageDraw.Draw(canvas)
draw.text((0, 0), text, font=font, fill="#000000")
canvas.show()
  • 7.1.1

7-1-1

  • 8.0.1

8-0-1

@radarhere
Copy link
Member

Thanks for letting us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Any unexpected behavior, until confirmed feature. Font Rendering
Projects
None yet
Development

No branches or pull requests

3 participants