首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >im.paste()不返回?

im.paste()不返回?
EN

Stack Overflow用户
提问于 2016-02-01 08:56:10
回答 1查看 1.4K关注 0票数 0
代码语言:javascript
运行
复制
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

我一直在做一些调试,这里唯一的错误是,由于某种原因,粘贴变量最终是空的,我不知道原因。它抛出此错误:

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-01 20:45:19

paste方法实际上什么也不返回,而是改变了图片的位置。例如见这里这里。您可以将代码更改为:

代码语言:javascript
运行
复制
image.paste(thumbed,tup)           # nothing is returned, so no assignment
final = ImageTk.PhotoImage(image)  # using image instead of pasted

当然,这意味着您的image将被更改,因此您可能希望在粘贴之前创建一个副本。

此外,正如@furas所指出的,您似乎根本不需要粘贴:您将image[index]的大小调整为与image相同的大小,然后将其粘贴到跨越整个大小的image中。相反,您应该能够直接使用调整大小的图片。

代码语言:javascript
运行
复制
thumbed = images[index].resize((x,y), PIL.Image.ANTIALIAS)
final = ImageTk.PhotoImage(thumbed)

但是,这不会改变image,所以如果您想要更改它,就必须粘贴。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35126363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档