Python pillow/PIL doesn't recognize the attribute "textsize" of the object "imagedraw"
Asked Answered
F

6

8

I already checked python version on my environment (sublime text) and it is 3.11.0, the latest, I checked pillow version which is 10.0.0, the latest, and my code looks similar to other examples online.

the code has a part in Italian, but its pretty understandable.

the problem is at "disegno.textsize(testo, font=font)

after I run the code:

line 14, in metti_testo_su_sfondo
    text_width, text_height = disegno.textsize(testo, font=font)
                              ^^^^^^^^^^^^^^^^
AttributeError: 'ImageDraw' object has no attribute 'textsize'

its strange because imagedraw should have the textsize attribute. I'm a novice, I hope I didn't miss anything blatant


from PIL import Image, ImageDraw, ImageFont

def metti_testo_su_sfondo(testo, sfondo, posizione=(10, 10), colore_testo=(0, 0, 0), dimensione_font=25):
# Apri l'immagine dello sfondo
immagine_sfondo = Image.open(sfondo)


disegno = ImageDraw.Draw(immagine_sfondo)


font = ImageFont.truetype("ARIAL.TTF", dimensione_font)


text_width, text_height = disegno.textsize(testo, font=font)

# Calcola le coordinate del testo centrato
x = (immagine_sfondo.width - text_width) // 2
y = (immagine_sfondo.height - text_height) // 2


disegno.text((x, y), testo, fill=colore_testo, font=font)


immagine_sfondo.save("spotted.png")


testo_da_inserire = "Ciao, mondo!"
sfondo_da_utilizzare = "spotted_bianco.jpg" 

metti_testo_su_sfondo(testo_da_inserire, sfondo_da_utilizzare)

The objective is a code that makes me images automatically without needing to edit them manually. I checked build system, python version and pillow version. when I run the code through cmd though it gives me this error:

from PIL import Image, ImageDraw, ImageFont
ModuleNotFoundError: No module named 'PIL'
Frediafredie answered 4/9, 2023 at 13:27 Comment(7)
You are probably not using the Python version into which you installed Pillow. Try adding this at the start of your script to see which Python you are actually running import sys; print(sys.executable, sys.version, *sys.path, sep="\n") Then, outside of your IDE, check where you installed PillowWrennie
thank you Mark, but unfortunately i dont think i solved anything. C:\Python311\python.exe 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] C:\Users\user\Desktop\text files\spotted C:\Python311\python311.zip C:\Python311\Lib C:\Python311\DLLs C:\Python311 C:\Python311\Lib\site-packages C:\Python311\Lib\site-packages\vboxapi-1.0-py3.11.egg python seems to be the right version, i also updated my build system the same day pillow is installed in python311\lib\site-packages C:\Python311\Lib\site-packages\Pillow-10.0.0.dist-infoFrediafredie
i think i will try to uninstall and reinstall pillow, then ill try with a virtual machineFrediafredie
i also tried using the textsize attribute in another code just to make sure from PIL import Image, ImageDraw, ImageFont b = Image.open("spotted_bianco.jpg") a = ImageDraw.Draw(b) c, d = a.textsize("e", font="ARIAL.TTF") i tried running it c, d = a.textsize("e", font="ARIAL.TTF") ^^^^^^^^^^ AttributeError: 'ImageDraw' object has no attribute 'textsize' [Finished in 161ms] and ofc it didnt workFrediafredie
i solved it. i decided to run it on a kali vm, and it told me that in pillow 10, the most recent version wich came out 7 days ago, textsize was going to be deprecated. ill fix my code, thank you for helping me.Frediafredie
once i solve my problem will moderators mark it as solved or do i need to do something? i cant find a "mark as solved" button anywhereFrediafredie
@Frediafredie Please write an Answer (form is below) with the solution and detailing the steps you took to fix your code. After a period of time you will be able to mark it as the correct answer for your question.Bohemia
F
16

textsize was deprecated, the correct attribute is textlength which gives you the width of the text. for the height use the fontsize * how many rows of text you wrote.

Frediafredie answered 9/9, 2023 at 23:38 Comment(2)
provide example code pleaseLeigh
Thank you. This is so far as backward compatibility is concerned in Python!Celestinacelestine
O
10

As other answers have mentioned, textsize is deprecated. There is no textheight, but you can use textlength.

If you just want a function that can test a given text and font pair, you can do something like this with textbbox:

def textsize(text, font):
    im = Image.new(mode="P", size=(0, 0))
    draw = ImageDraw.Draw(im)
    _, _, width, height = draw.textbbox((0, 0), text=text, font=font)
    return width, height

Which should work the same way.

Opsonin answered 3/1 at 1:28 Comment(0)
A
6

It's no longer called textsize, it's called textlength.

Arvo answered 8/9, 2023 at 21:51 Comment(0)
P
3

Use textlength instead.

Here is the example

from PIL import Image, ImageDraw,  ImageFont 
text='TEST'
font_size=16
font = ImageFont.truetype(r"path\OpenSans-Bold.ttf", font_size)
draw = ImageDraw.Draw(your_img)
textheight = font_size
textwidth = draw.textlength(text,font)  # don't forget to add font
Pinero answered 9/8 at 10:47 Comment(0)
M
0
pip install pillow==9.5.0

version=9.5.0

It can still be used, but a warning will appear Use textbbox or textlength instead.

  c_width, c_height = draw.textsize(c, font=font)
Mystique answered 7/3 at 12:11 Comment(0)
N
-1

You can also use the font.getbbox(text) and it will returned a 4 element tuple (x1,y1,x2,y2)

width , heigth = (x2-x1, y2-y1)

Newhouse answered 21/2 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.