-
Hello! Is there max width alternative like in canvas? So when I set it it compress text if it too big |
Beta Was this translation helpful? Give feedback.
Answered by
radarhere
Jul 8, 2025
Replies: 1 comment 1 reply
-
We don't have an API specifically for that purpose, but users can still use our other APIs to achieve the end result. from PIL import Image, ImageDraw, ImageFont
im = Image.new("RGB", (250, 150), "#fff")
d = ImageDraw.Draw(im)
font = ImageFont.load_default(size=48)
# Normal
d.text((0, 0), "Hello world", "#000", font)
# With max-width
maxwidth = 100
left, top, right, bottom = d.textbbox((0, 0), "Hello world", font)
width = right - left
if width > maxwidth:
im2 = Image.new("RGBA", (right, bottom))
d = ImageDraw.Draw(im2)
d.text((0, 0), "Hello world", "#000", font)
im2 = im2.resize((maxwidth, bottom))
im.paste(im2, (0, 75), mask=im2)
else:
d.text((0, 75), "Hello world", "#000", font)
im.show() |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Nazar1ky
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We don't have an API specifically for that purpose, but users can still use our other APIs to achieve the end result.