Andrzej Krynski
Andrzej Krynski

Reputation: 71

How to use camera on packed Tkinter canvas (opencv)

could someone explain me why after modyfication of commented out code there is no camera view more? I get error message

" self.a = Image.fromarray(self.capture)#PIL 1.6# AttributeError: class Image has no attribute 'fromarray'"

from x.update_video method. Commented code is from topic OpenCV (cv2 in Python) VideoCapture not releasing camera after deletion I am very new to Tkinter and found no help in tutorials. I suppose it is due to frame is packed as well as canvas. But there must be any solution...

class App:
      def __init__(self, master,cam):
##    root = tk.Tk()
##    videoframe = tk.LabelFrame(root,text='Captured video')
##    videoframe.grid(column=0,row=0,columnspan=1,rowspan=1,padx=5, pady=5, ipadx=5, ipady=5)
##    canvas = tk.Canvas(videoframe, width=640,height=480)
##    canvas.grid(column=0,row=0)
##    cam = cv2.VideoCapture(2)
##    x = vid(cam,root,canvas)
##    root.after(0,x.update_video)
##    button = tk.Button(text='Quit',master=videoframe,command=root.destroy)
##    button.grid(column=0,row=1)
##    root.mainloop()
##    del cam
        frame = LabelFrame(master,text='Captured video')
        frame.pack()
        canvas = Canvas(frame, width=640,height=480)
        canvas.pack(side=TOP)

        x = vid(cam,master,canvas)
        master.after(0,x.update_video)

        self.button = Button(frame, text="QUIT", fg="red", command=master.destroy)
        self.button.pack(side=LEFT)
        self.hi_there = Button(frame, text="Hello", command=self.say_hi)
        self.hi_there.pack(side=LEFT)
      def say_hi(self):
        print "hi there, everyone!"
root = Tk()
cam = cv2.VideoCapture(0)
app = App(root,cam)
root.mainloop()
del cam

Upvotes: 0

Views: 4455

Answers (3)

Andrzej Krynski
Andrzej Krynski

Reputation: 71

Brayan's answer was a bit confusing me, but we is right. My problem was caused by include. Never use import *. It overwrote Image class from PIL module with Image class from Tkinter and this class has no such method. Thanks.

Upvotes: 0

Andrzej Krynski
Andrzej Krynski

Reputation: 71

My image is a capture from camera.

 def update_video(self):
        (self.readsuccessful,self.f) = self.cam.read()
        self.capture = cv2.cvtColor(self.f, cv2.COLOR_RGB2RGBA) #cv2.COLOR_RGB2GRAY)
        self.a = Image.fromarray(self.capture)#nowe w PIL 1.6 #PIL.Image.VERSION=1.1.7
        self.b = ImageTk.PhotoImage(image=self.a) ...

It works in commented version.` Though Image class has the method fromarray although is not yet well docummented.

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 385980

The error message is telling you precisely what the problem is: you're trying to call fromarray on a class that has no such method. That probably means that Image is not what you think it is, so start by verifying for yourself exactly what Image is, and whether it's supposed to have an fromarray method.

Upvotes: 1

Related Questions