AttributeError:“PhotoImage”对象没有属性“size”

2024-06-28 19:36:49 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,我打开并使用

img = Image.open("C:\\users\\zouaoua\\PycharmProjects\\image\\lenna.png")
photo = ImageTk.PhotoImage(img)
canvas.config(height=photo.height(), width=photo.width())
canvas.create_image(0, 0, anchor=NW, image=photo)
canvas.pack()

我想以我使用的负片方式重新拍摄图像

def retation_negative(image):
global im

L, H = image.size
im2 = Image.new("RGB", (L, H))
for y in range(H):
    # print(x)
    for x in range(L):
        p = image.getpixel((y, x))
        im2.putpixel((x, y), p)
im = ImageTk.PhotoImage(im2)
canvas.config(height=im.height(), width=im.width())
canvas.create_image(0, 0, anchor=NW, image=im)

submenu = Menu(second_menu, tearoff=0)
submenu.add_command(label="negatve", command=retation_negative(img))

但是当我运行代码时,我得到了这个错误,我不知道为什么我这么困惑

Traceback (most recent call last):
File "C:\Users\zouaoua\PycharmProjects\image\img.py", line 241, in <module>
submenu.add_command(label="Positive", command=retation_negative(img))
File "C:\Users\zouaoua\PycharmProjects\image\img.py", line 71, in retation_negative                                     
L, H = image.size
AttributeError: 'PhotoImage' object has no attribute 'size'

Tags: inimageimgsizewidthcommandcanvasheight