Thursday 7 January 2021

How to set the size in pixels in the truetype() function of the Pillow library?

How can I keep the actual text size the same for different fonts. That is, I want the text written by the ImageDraw.Draw().Text() function to be the same size, for example 48 pixels. To specify the size of the text, I pass the number of POINTS to the ImageFont.TrueType() function, but the points for each font are different. Here's a quick summary: if the size of the text written in the font Arial = 48 pixels and = 56 points, this does not mean that the text written in Times New Romans with a size of 56 points will be equal to 48 pixels. My task is to make sure that I can somehow translate these 48 pixels into n points for any font.

I Added a picture to clearly convey my problem enter image description here

This is the REPREX of my code

from PIL import Image, ImageDraw, ImageFont

line_height = 48
image = Image.new('RGB', (60, 60), 'white')
drawer = ImageDraw.Draw(image)
drawer.line(((0, 8), (59, 8)), width=1, fill='black')
drawer.line(((0, 8 + line_height), (59, 8 + line_height)), width=1, fill='black')
font = ImageFont.truetype(r'C:\Windows\Fonts\ariali.ttf',
                          size=line_height)  # size should be in points but I don't now, how to do it
drawer.text((0, 8), 'help', fill='black', font=font)
image.show()


from How to set the size in pixels in the truetype() function of the Pillow library?

No comments:

Post a Comment