Pillow: Absent pixels when drawing font

Created on 20 Apr 2020  路  7Comments  路  Source: python-pillow/Pillow

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()
Bug Font Rendering

All 7 comments

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

@radarhere I place a zip here.

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.

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 https://github.com/python-pillow/Pillow/issues/4511#issuecomment-612940904, by enabling hinting for FT_GetGlyphCBox in _imagingft.getsize.

@nulano Nice!

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

Thanks for letting us know!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

damianmoore picture damianmoore  路  4Comments

SysoevDV picture SysoevDV  路  3Comments

naaaargle picture naaaargle  路  3Comments

readyready15728 picture readyready15728  路  4Comments

Larivact picture Larivact  路  4Comments