New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to handle textsize deprecation warning #6938
Comments
Does |
Yes, the new functions do not return the same results - the old function was broken and returning incorrect values. But I'm curious, you haven't mentioned what exactly is your problem with the new functions?
This is not a reliable way to center text (irrespective of how you obtain text width and height), it is preferred to use Whether you use Does either of these examples help? Using from PIL import Image, ImageDraw, ImageFont
w, h = 300, 100
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
im = Image.new("RGB", (w, h), "white")
draw = ImageDraw.Draw(im)
font_size = 40
font = ImageFont.truetype(r"C:\Users\Nulano\AppData\Local\Microsoft\Windows\Fonts\OpenSans-Bold.ttf", font_size)
while font_size > 1:
if font.getlength(text) < w:
break
font_size -= 1
font = font.font_variant(size=font_size)
draw.text((w / 2, h / 2), text, font=font, fill="black", anchor="mm")
im.show() Using from PIL import Image, ImageDraw, ImageFont
w, h = 300, 100
region = (50, 40, 200, 90)
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
im = Image.new("RGB", (w, h), "white")
draw = ImageDraw.Draw(im)
draw.rectangle(region, outline="black")
font_size = 40
font = ImageFont.truetype(r"C:\Users\Nulano\AppData\Local\Microsoft\Windows\Fonts\OpenSans-Bold.ttf", font_size)
x = int((region[2] - region[0]) / 2) + region[0]
y = int((region[3] - region[1]) / 2) + region[1]
while font_size > 1:
bbox = draw.textbbox((x, y), text, font=font, anchor="mm")
if bbox[0] > region[0] and bbox[1] > region[1] and bbox[2] < region[2] and bbox[3] < region[3]:
break
font_size -= 1
font = font.font_variant(size=font_size)
draw.text((x, y), text, font=font, fill="black", anchor="mm")
im.save(r"E:\6938-textbbox.png")
im.show() If you insist on using def getsize(...):
# this function has confusing behaviour and should not be used
left, top, right, bottom = font.getbbox(...)
width = right - left # this value can be confusing
height = bottom # this value is clearly wrong
return width, height but as you can see from that code, that function most likely does not return the values you expect. |
@ArthurPains did that answer your question? |
@radarhere Sorry for the delay, I was traveling. |
I'm getting textsize is deprecated, however when I use textbbox or textlength instead, they don't do the same.
After a lot of research, I finally managed to figure out how to recalculate the font size of a text if it doesn't fit in a specific area. But the only way I could do that was using "textsize". I've already tried using textbbox or textlength and I couldn't use them in my function at all. With that, every time the text size needs to decrease, this message appears to me:
I would like to know how can I deal with this? Because every time a print appears with this message, it bothers me and also makes it difficult for me to find other errors.
This is my function:
The text was updated successfully, but these errors were encountered: