Thursday, 6 June 2019

Make to make text size auto adjust to an image with PIL

I am trying to get some text to overlap on an image, which is I have the following code for.

from PIL import Image, ImageDraw, ImageFont

msg = "This is a test phrase, so please shrink the text."

im = Image.open("test.jpg")
draw = ImageDraw.Draw(im)

W, H = im.size

myFont =             
ImageFont.truetype("/usr/share/fonts/truetype/customfonts/KeepCalm-Medium.ttf")

w, h = draw.textsize(msg, font=myFont)
draw.text(((W-w)/2,(H-h)/2), msg, fill="black", font=myFont)

im.save("sample-out.png", "PNG")

What I need is the text to scale in the middle, but between the pixel width and height of 1600,300. Whichever one it achieve first.

I imagine it has something to do with fontsize increasing, but I can't figure it out.



from Make to make text size auto adjust to an image with PIL

No comments:

Post a Comment