python - Filedialog python的缩略图问题
问题描述
我遇到了一个问题self.photo = ImageTk.PhotoImage(self.resized_img)
。它告诉我AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
。我用缩略图功能做错了吗?
def fileDialog(self):
self.filename = filedialog.askopenfilename(title="Select file")
self.label = ttk.Label(self.labelFrame, text = "")
self.label.grid(column = 1, row = 2)
self.label.configure(text=os.path.basename(self.filename))
self.img = Image.open(self.filename)
self.thumbNail_img = self.img.thumbnail((512, 512))
self.photo = ImageTk.PhotoImage(self.thumbNail_img)
self.display = ttk.Label(image=self.photo)
self.display.place(relx=0.10, rely=0.10)
错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "gui.py", line 44, in fileDialog
self.photo = ImageTk.PhotoImage(self.resized_img)
File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PIL\ImageTk.py", line 113, in __init__
mode = Image.getmodebase(mode)
File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PIL\Image.py", line 326, in getmodebase
return ImageMode.getmode(mode).basemode
File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PIL\ImageMode.py", line 56, in getmode
return _modes[mode]
解决方案
尝试
print(type(self.img), type(self.thumbNail_img))
你看
<class 'PIL.JpegImagePlugin.JpegImageFile'> <class 'NoneType'>
这意味着比self.img
是Image
但是self.thumbNail_img
None
thumbnail()
不创建新图像。它更改原始图像self.img
并返回None
。
它有效"in-place"
。文档:Image.thumbnail
所以你必须使用self.img
来显示它
self.img = Image.open(self.filename)
self.img.thumbnail((512, 512))
self.photo = ImageTk.PhotoImage(self.img)
如果您需要原始图像,那么您.copy()
可以
self.img = Image.open(self.filename)
self.thumbNail_img = self.img.copy()
self.thumbNail_img.thumbnail((512, 512))
self.photo = ImageTk.PhotoImage(self.thumbNail_img)
推荐阅读
- javascript - 有没有办法根据 IFRAME 中的文本字符串制作 IF 语句?
- javascript - 属性和子属性未定义检查
- ssl - 为什么在尝试执行此 cURL 请求时会出现此证书错误?SSL:没有替代证书主题名称与目标主机名匹配
- python - “NoneType”对象在获取谷歌结果时没有属性“文本”
- python - 当有回溯时显示回溯。什么是 EOF 错误
- c++ - 从 ppm 文件中读取 RGB 值并使用结构(动态数组)将它们存储到名为“Image”的二维数组中
- python - 为什么这段代码在pygame的主循环之外工作
- sql - 检查表中的属性是否具有特定值 SQL
- amazon-web-services - Amazon EC2 实例 - 我上传的文件在哪里?
- jekyll - 杰基尔实际上是做什么的?