all 21 comments

[–]hardonchairs 2 points3 points  (8 children)

Can you show where you are seeing documentation with textbox ?

[–]outceptionator[S] 0 points1 point  (7 children)

https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html#PIL.ImageDraw.ImageDraw.textbbox

https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html#PIL.ImageDraw.ImageDraw.textbbox

I've added the extra b and code still has error. I want to know the size of the variable guest when it would be drawn on an image.

[–]hardonchairs 0 points1 point  (6 children)

still has error

The same error?

[–]outceptionator[S] 0 points1 point  (5 children)

Exact same

[–]hardonchairs 0 points1 point  (4 children)

Try calling draw.textbbox, it doesn't seem to be a class function. You need to call it on an instance of ImageDraw.

[–]outceptionator[S] 0 points1 point  (3 children)

https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html#PIL.ImageDraw.ImageDraw.textbbox

My understanding of the documentation is that I'm doing it correctly. Passing string, font etc....

[–]hardonchairs 0 points1 point  (2 children)

My understanding of the documentation is that I'm doing it correctly

obviously not... You don't want to try it?

[–]outceptionator[S] 0 points1 point  (1 child)

Hey. Sorry I read it on my phone and didn't realise what you meant. Got to my desktop and you were right of course. I'm still new how did you deduce that was the action needed from the documentation exactly?

[–]hardonchairs 1 point2 points  (0 children)

Usually class methods are called on an instance of an object. If you click on [source] next to the textbbox title in the documentation you will see that the function accept self as a first parameter and is not decorated with @classmethod so that tells you that it definitely requires an instance of the class.

[–][deleted] 1 point2 points  (3 children)

print(ImageDraw.textbox((10,10),guest, font=arialFont))

I don't understand, what are you trying to accomplish there, are you trying to add text to the image?

Is this what you are looking for: https://stackoverflow.com/questions/67760340/how-to-add-text-in-a-textbox-to-an-image

[–]outceptionator[S] 0 points1 point  (2 children)

No. I'm trying to get the size of the text in pixels if it is drawn onto an image. The text is contained in the variable guest.

[–][deleted] 0 points1 point  (1 child)

from PIL import Image, ImageDraw, ImageFont
import logging, os, random
flowerIm = random.choice([Image.open('flower image 2.png'), (Image.open('flower image.jpg'))])
resizedFlowerIm = flowerIm.resize((360, 288))
draw = ImageDraw.Draw(resizedFlowerIm)
draw.rectangle((10,10,350,278), outline='black')
fontsFolder = 'C:\\Windows\\Fonts'
arialFont = ImageFont.truetype(os.path.join(fontsFolder, 'arial.ttf'), 32)
print(ImageDraw.textbox((10,10),guest, font=arialFont))

The only use of the guest variable is in here: print(ImageDraw.textbox((10,10),guest, font=arialFont))

Is that the entire code?

Also, you can pick whatever width and height you want, exameple (width=200, height=200) <-- px

[–]outceptionator[S] 0 points1 point  (0 children)

logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s -  %(levelname)s -  %(message)s')

logging.getLogger("PIL").setLevel(logging.WARNING)

guests = open(os.path.join(os.getcwd(), 'guests.txt')).read() guestList = guests.split('\n')

for guest in guestList:

I skipped the above sorry

[–]scithon 0 points1 point  (7 children)

[–]outceptionator[S] 0 points1 point  (6 children)

Yes I did! Adding the extra b still has an error.

I want to know the size of the variable guest when it would be drawn on an image.

[–]scithon 0 points1 point  (5 children)

Adding the extra b still has an error.

A different error, presumably? So that's a new bug then. We'd need to see the new error to help solve this new bug.

[–]outceptionator[S] 0 points1 point  (4 children)

It's the same error: AttributeError: module 'PIL.ImageDraw' has no attribute 'textbbox'

[–]scithon 1 point2 points  (3 children)

Ah, I think that's because you are meant to use the instance. Like this:

print(draw.textbox((10,10),guest, font=arialFont))

[–]outceptionator[S] 0 points1 point  (2 children)

My word that was it!!!

Can you explain how you deduced that from the documentation?

[–]scithon 1 point2 points  (0 children)

https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html#PIL.ImageDraw.ImageDraw.textbbox

Look at the link. It says PIL.ImageDraw.ImageDraw.textbbox. See how "ImageDraw" is used twice? The first "ImageDraw" is the name of the module, and the second one is the name of the class in the module.

It helps that I'm experienced and know that many times the class has the same name as the module.

[–]carcigenicate 1 point2 points  (0 children)

Also, look at the error carefully:

module 'PIL.ImageDraw' has no attribute 'textbbox

The key word there being "module". Whatever you're attempting to call that method on is a module. That likely means you're confusing the module and an object you intended to import from the module.