首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python : AttributeError:'PhotoImage‘对象没有属性'_PhotoImage__photo’

Python : AttributeError:'PhotoImage‘对象没有属性'_PhotoImage__photo’
EN

Stack Overflow用户
提问于 2020-10-25 11:04:24
回答 1查看 1.4K关注 0票数 0

编辑:当我使用image = ImageTk.PhotoImage(resize_image)而不是file=resize_image时,画布上什么都不会显示。见截图

我正在创建一个图像查看器,它被分割成四个帧,每个帧分别显示一个图像。目前,我对四个框架中的3个框架进行了评论,重点是首先修复异常。我试图在画布中显示一个图像,后者嵌入在屏幕左上角的frame_1中。每当我运行我的代码时,我都会得到堆栈跟踪:

代码语言:javascript
复制
Traceback (most recent call last):
  File "c:/Users/EM/Desktop/Scripts/gui/slideshow_model/slide_show_class.py", line 67, in <module>
    slideshow_model.show_image(img1)
  File "c:/Users/EM/Desktop/Scripts/gui/slideshow_model/slide_show_class.py", line 55, in show_image
    image = ImageTk.PhotoImage(file=resize_image)
  File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 89, in __init__
    image = _get_image_from_kw(kw)
  File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 58, in _get_image_from_kw
    return Image.open(source)
  File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2852, in open
    prefix = fp.read(16)
AttributeError: 'Image' object has no attribute 'read'
Exception ignored in: <function PhotoImage.__del__ at 0x0000017EAD432D08>
Traceback (most recent call last):
  File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 118, in __del__
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'

这是我的班级:

代码语言:javascript
复制
import tkinter as tk
from PIL import ImageTk, Image

root = tk.Tk()


class SlideshowModel():

    def __init__(self, master):
        self.master = root
        master.title('Basic Image Viewer')
        root.iconbitmap('../img/favicon.ico')
        root.state('zoomed')
        s_w = int(root.winfo_screenwidth())
        s_h = int(root.winfo_screenheight())
        self.grid_w = s_w // 2
        self.grid_h = s_h // 2
        self.frame_1 = tk.Frame(master, height=self.grid_h,
                                width=self.grid_w, bd=0)
        self.frame_1.grid(column=0, row=0)
        self.can = tk.Canvas(
            self.frame_1, width=(self.grid_w - 50), height=(self.grid_h - 50), bg="red")
        self.can.grid(row=0, column=0)
        self.frame_2 = tk.Frame(master, height=self.grid_h,
                                width=self.grid_w, bd=0, bg="black")
        self.frame_2.grid(column=1, row=0)
        self.frame_3 = tk.Frame(master, height=self.grid_h,
                                width=self.grid_w, bd=0, bg="black")
        self.frame_3.grid(column=0, row=1)
        self.frame_4 = tk.Frame(master, height=self.grid_h,
                                width=self.grid_w, bd=0, bg="black")
        self.frame_4.grid(column=1, row=1)


    # should return the image object
    def resize_image(self, img_path):
        image = Image.open(img_path)
        w_coeff = image.width / self.grid_w
        h_coeff = image.height / self.grid_h
        w_coeff = 1 / w_coeff if w_coeff > 1 else w_coeff
        h_coeff = 1 / h_coeff if h_coeff > 1 else h_coeff
        # pick the smallest coeff to get the image as small
        # as should be
        coeff = min(w_coeff, h_coeff)
        image = image.resize(
            (int(image.width * coeff), int(image.height * coeff)), Image.ANTIALIAS)
        return image

    # this function should show returned image
    # takes: image object, master frame
    def show_image(self, resize_image):
        image = ImageTk.PhotoImage(resize_image)
        # label = tk.Label(frame_x, image=image, bd=0)
        self.can.create_image(0, 0, image=image, anchor='nw')


slideshow_model = SlideshowModel(root)

img1 = slideshow_model.resize_image('../img/sample.jpg')

slideshow_model.show_image(img1)

root.mainloop()

我遗漏了什么?更新:现在窗口毫无例外地打开,但是图像仍然没有显示,我将canvas涂上红色,并将其宽度和高度减少了50 it,以便能够将其与框架区分开来,而后者是彩色的(灰色/白色)。

EN

回答 1

Stack Overflow用户

发布于 2020-10-25 18:18:24

问题是我的image = Image.open(...)正在被垃圾收集,所以我不得不添加self.,这样它才不会被销毁。请参阅下面的代码片段:

代码语言:javascript
复制
    def resize_image(self, img_path):
        self.image = Image.open(img_path)
        w_coeff = self.image.width / self.grid_w
        h_coeff = self.image.height / self.grid_h
        w_coeff = 1 / w_coeff if w_coeff > 1 else w_coeff
        h_coeff = 1 / h_coeff if h_coeff > 1 else h_coeff
        # pick the smallest coeff to get the image as small
        # as should be
        coeff = min(w_coeff, h_coeff)
        self.image = self.image.resize(
            (int(self.image.width * coeff), int(self.image.height * coeff)), Image.ANTIALIAS)
        return self.image

    def show_image(self, resize_image):
        self.image_tk = ImageTk.PhotoImage(resize_image)
        self.can.create_image(0, 0, image=self.image_tk, anchor='center')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64522972

复制
相关文章

相似问题

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