Pixel size of a Python String

Asked

Viewed 169 times

-1

I would like to know how to make a code in Python that checks the size of a string (in pixels).

Guess: we have a string 'ALCON BOTTON FISH 50G' I would like to know what its pixel size to center in a square I’m filling with reportlab (I’m using absolute plotting - informing the x and y position - for not finding an automatic way to do this centralization)

  • If the source is monospace maybe I can, if it’s not gonna get complicated.

  • Was your question "pixel size" a little confusing? the size of a pixel will depend on the resolution and/or the device being displayed to the image, no matter if the image represents a string or a bird, the same string that vc da as an example can be "built", and/or plotted in different resolutions (pixels per inch), forget the string represented in the image and focus on the image characteristics (size, resolution, etc)

  • Some technologies, like Qt, have this feature, but it only makes sense using the same functions of who will plot/draw the text in definitive. You have to see if this reportlab has a proper function.

1 answer

1


There is no single calculation, as the resulting size depends on the source. In Arial 12 is a size, in Times New Roman 12 will give another... if use bold, changes also.

There is still Kerning, which is the difference in spacing between two letters, depending on which letters are:

kerning

It is therefore difficult to calculate this without drawing the source. An example, in Pillow has the function ImageFont.getsize():

>>> arial = PIL.ImageFont.truetype('Arial', 12)
>>> arial.getsize('Palavra')
(42, 11)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.