0

For drawing a text with a custom font on a image using pillow we use truetype function. But when I use that and I specify the fontsize, the text which is drawn is not really the exact fontsize which it should've been.

For eg:

 
draw = ImageDraw.Draw(image) 
  
# specified  size
font = ImageFont.truetype(r'devangiri.ttf', 20) 
  
text = 'SOMEWORDS'
  
# drawing text size
draw.text((5, 5), text, font = font)

When I cross check the output image, and calculate the font size, it is not really equal to 20px which it should've been. As font size is merely height of text, when I calculate that height, both are not same by big values.

How to calculate correct font size to write in truetype ?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Arnav Mehta
  • 67
  • 1
  • 7
  • Does this answer your question? [How is font size calculated?](https://stackoverflow.com/questions/3495872/how-is-font-size-calculated) – Cris Luengo Sep 23 '22 at 14:13
  • ...though that answer doesn't take into account that some fonts are designed to extend outside the em box, and some are designed to never go near its edges. Basically, it is the em box that you size when choosing a font size. Each font will appear to have a different size for a given size em box. What you really want to do often is specify the x height. The only way to do so properly is rendering an x (or an o, or some other lower-case letter without ascenders or descenders) and measuring its height. – Cris Luengo Sep 23 '22 at 14:20
  • how @CrisLuengo please – Arnav Mehta Sep 23 '22 at 15:11
  • I had tried converting pt to px, by multiplying by 3/4 but no changes :( – Arnav Mehta Sep 23 '22 at 15:11
  • I don't know PIL well enough to show you how to do that with PIL. But do you understand the explanation for why the font has a different size than you'd expect? – Cris Luengo Sep 23 '22 at 15:47
  • Um, yes, but there must be some way – Arnav Mehta Sep 24 '22 at 08:21

0 Answers0