【问题标题】:Python/OpenCV Tkinter ExceptionsPython/OpenCV Tkinter 异常
【发布时间】:2017-04-27 01:43:30
【问题描述】:

尊敬的,

我是 python 新手,我正在使用 python/opencv 开发一个 gui,它显示两个图像。一份原件,一份加工(边缘显示)。我的程序因错误而崩溃。以下是代码: `

from mtTkinter import *  
import tkFileDialog
from PIL import ImageTk
from PIL import Image
import cv2

def select_image():
    global  panelA, panelB
    path = tkFileDialog.askopenfilename()

    if len(path) > 0:
        image = cv2.imread(path)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        edges = cv2.Canny(gray, 50, 100)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        image = Image.fromarray(image)
        edges = Image.fromarray(edges)

        image = ImageTk.PhotoImage(image)
        edges = ImageTk.PhotoImage(image)

        if panelA is None or panelB is None:
            panelA = Label(image=image)
            panelA.image = image
            panelA.pack(side=LEFT, padx=10, pady=10)


            panelB = Label(image=edges)
            panelB.image = edges
            panelB.pack(side=RIGHT, padx=10,pady=10)

        else:
            panelA.configure(image=image)
            panelB.configure(image=edges)
            panelA.image = image
            panelB.image = edges
root = Tk()
panelA = None
panelB = None


btn = Button(root, text="Select an image", command=select_image)
btn.pack(side="bottom", fill="both", expand="yes", padx="10", pady="10")

root.mainloop()

` 错误是:

   `C:\Python27\python.exe C:/Users/BlackHat/PycharmProjects/example/init.py
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1542, in __call__
    return self.func(*args)
  File "C:/Users/BlackHat/PycharmProjects/example/init.py", line 21, in select_image
    edges = ImageTk.PhotoImage(image)
  File "C:\Python27\lib\site-packages\PIL\ImageTk.py", line 108, in __init__
    mode = Image.getmodebase(mode)
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 295, in getmodebase
    return ImageMode.getmode(mode).basemode
  File "C:\Python27\lib\site-packages\PIL\ImageMode.py", line 52, in getmode
    return _modes[mode]
KeyError: <PIL.ImageTk.PhotoImage object at 0x02DD05B0>
Exception in Tkinter callback
Exception AttributeError: "'PhotoImage' object has no attribute '_PhotoImage__photo'" in <bound method PhotoImage.__del__ of <PIL.ImageTk.PhotoImage object at 0x02DD0790>> ignored
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1542, in __call__
    return self.func(*args)
  File "C:/Users/BlackHat/PycharmProjects/example/init.py", line 21, in select_image
    edges = ImageTk.PhotoImage(image)
  File "C:\Python27\lib\site-packages\PIL\ImageTk.py", line 108, in __init__
    mode = Image.getmodebase(mode)
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 295, in getmodebase
    return ImageMode.getmode(mode).basemode
  File "C:\Python27\lib\site-packages\PIL\ImageMode.py", line 52, in getmode
    return _modes[mode]
KeyError: <PIL.ImageTk.PhotoImage object at 0x02DD0890>
Exception AttributeError: "'PhotoImage' object has no attribute '_PhotoImage__photo'" in <bound method PhotoImage.__del__ of <PIL.ImageTk.PhotoImage object at 0x02DD0850>> ignored

Process finished with exit code 1`

在 stackoverflow 上有两个或更多这样的查询,但我没有找到问题的正确解决方案。

请帮忙。谢谢

【问题讨论】:

    标签: windows python-2.7 opencv tkinter pycharm


    【解决方案1】:

    你的问题是这一行:

    块引用

        image = Image.fromarray(image)
        edges = Image.fromarray(edges)
    
        image = ImageTk.PhotoImage(image)
       >>edges = ImageTk.PhotoImage(**image**)<<
    

    块引用

    块引用

        edges = ImageTk.PhotoImage(edges) 
    

    块引用

    是应该说的。您在此处将 PhotoImage 转换为 PhotoImage。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-19
      • 2017-04-26
      • 2012-12-10
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      相关资源
      最近更新 更多