G
G
Guerro692020-09-14 11:25:03
Python
Guerro69, 2020-09-14 11:25:03

Pillow, how to proportionally adjust text size?

I wrote the code for image processing, but besides this, I also made it so that my VK was written at the bottom of the processed image, so that any person knew who I was .. the problem is that if a person wants to process an image smaller than 250x250, then this signature was written in large letters for the whole picture, which is quite logical, I tried to come up with a formula in order to choose a proportional text size for the photo, but it didn’t work out very well.
Here is my code:

from PIL import Image, ImageDraw, ImageFont

image = Image.open("images\\ans_2.png")
draw = ImageDraw.Draw(image)
width = image.size[0]
height = image.size[1]

def painting():
  size = 30
  font = ImageFont.truetype('images\\guer.ttf', size=size)
  text = 'vk.com/мой_вк'
  x = width - 100
  x -= size/2 * len(list(text))/2
  draw.text((x, height-50), text, (255,255,255), font)

painting()
image.save("images\\new.png")
del draw

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor T2, 2020-09-14
@Guerro69

The font size is proportional to the image height:
size = int(height / k)
k - empirical coefficient

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question