2016-12-12 30 views
0

について、Python/OpenCV Tkinter例外

私はpythonには新規です。私はpython/gucciを使って2つの画像を表示するGUIを開発しています。 1つのオリジナルと他の1つが処理されます(エッジ表示)。私のプログラムはエラーでクラッシュします。 `

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` 

あり、このような2つの以上のクエリはstackoverflowの上にあったが、私は問題の適切な解決策を見つけることができませんでした。

助けてください。おかげ

答えて

0

はあなたの問題は、この行です:

BLOCKQUOTE

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

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

BLOCKQUOTE

BLOCKQUOTE

edges = ImageTk.PhotoImage(edges) 

BLOCKQUOTE

があると言うべきものです。ここでPhotoImageをPhotoImageに変換しています。

関連する問題