Skip to content

How to resize the font based on the length of the text? #6891

Answered by radarhere
giggioman00 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi. Pillow doesn't have an autoresize function, but it does have functions to determine how large text will be once it is drawn - you might be interested in getlength(), getbbox() or multiline_textbbox().

Here's a demonstration.

from PIL import Image, ImageDraw, ImageFont
import urllib.request

req = urllib.request.urlopen("https://python-pillow.org/dist/5d1875f38a1f87b7dbc3.png")
width = 750
height = 500
im = Image.open(req).resize((width, height))
draw = ImageDraw.Draw(im, "RGBA")

quote = "Pillow is the friendly PIL fork by Alex Clark and Contributors. PIL is the Python Imaging Library by Fredrik Lundh and Contributors. As of 2019, Pillow development is supported by Tidelift. The Pytho…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@radarhere
Comment options

@Tetrax-10
Comment options

@nulano
Comment options

Answer selected by radarhere
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
4 participants