def changeImage(event,label,images,image):
w = event.widget
index = int(w.curselection()[0])
x,y = image.size
thumbed = images[index].resize((x,y),PIL.Image.ANTIALIAS)
print str(thumbed)
tup = (0,0,x,y)
paste = image.paste(thumbed,tup)
final = ImageTk.PhotoImage(paste)
label.config(image=final)
label.image = final
我一直在做一些调试,唯一不对的是,由于某种原因,粘贴变量原来是无,我无法弄清楚原因。它抛出了这个错误:
Exception in Tkinter callback
Exception AttributeError: "'PhotoImage' object has no attribute '_PhotoImage__photo'" in <bound method PhotoImage.__del__ of <PIL.ImageTk.PhotoImage object at 0x000000000A4C7208>> ignored
Traceback (most recent call last):
File "C:\Users\Alec\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.6.1.3253.win-x86_64\lib\lib-tk\Tkinter.py", line 1536, in __call__
return self.func(*args)
File "C:\Users\Alec\Desktop\Bhimisti Frames Software\Bhimisti_Frames-ver3.py", line 62, in <lambda>
frames.bind("<<ListboxSelect>>", lambda event,arg=imLabel,arg2=images,arg3=image: changeImage(event,arg,arg2,arg3))
File "C:\Users\Alec\Desktop\Bhimisti Frames Software\Bhimisti_Frames-ver3.py", line 35, in changeImage
final = ImageTk.PhotoImage(paste)
File "C:\Users\Alec\AppData\Local\Enthought\Canopy\User\lib\site-packages\PIL\ImageTk.py", line 108, in __init__
mode = Image.getmodebase(mode)
File "C:\Users\Alec\AppData\Local\Enthought\Canopy\User\lib\site-packages\PIL\Image.py", line 297, in getmodebase
return ImageMode.getmode(mode).basemode
File "C:\Users\Alec\AppData\Local\Enthought\Canopy\User\lib\site-packages\PIL\ImageMode.py", line 52, in getmode
return _modes[mode]
KeyError: None
答案 0 :(得分:0)
paste
方法确实没有返回任何内容,只是改变了原位图片。参见例如here和here。您可以将代码更改为:
image.paste(thumbed,tup) # nothing is returned, so no assignment
final = ImageTk.PhotoImage(image) # using image instead of pasted
当然,这意味着您的image
将会被更改,因此您可能希望在粘贴之前创建副本。
另外,正如@furas所指出的那样,您似乎根本不需要粘贴:您将image[index]
调整为与image
相同的尺寸,然后将其粘贴到{{1跨越整个大小。相反,您应该能够直接使用调整大小的图片。
image
然而,这将不更改thumbed = images[index].resize((x,y), PIL.Image.ANTIALIAS)
final = ImageTk.PhotoImage(thumbed)
,因此如果您想要更改它,则必须粘贴。