Hallo
Die aufgenommenen Fotos werden zu einem Film zusammengesetzt.
Code: Alles auswählen
import Tkinter as tk
from PIL import Image, ImageTk
import cv
UPDATE_TIME = 10
WIDTH = 600
HEIGHT = 480
RGB = "RGB"
BGR = "BGR"
MODUS = "raw"
MOVIE_NAME = "output.avi"
BUTTON_TEXT = "OK"
BUTTON_WIDTH = 10
FPS = 30
CAM = 1
class Cam(tk.Label):
    def __init__(self, root, width, height, cam, movie_file):
        tk.Label.__init__(self, root)
        self.width = width
        self.height = height
        self.root = root
        self.writer = cv.CreateVideoWriter(filename = movie_file, fourcc=
           cv.CV_FOURCC("D","I","V","X"), fps=FPS, frame_size=(width ,
           height))
        self.camera = cv.CaptureFromCAM(cam)
        
    def run(self):
        self.ipl_image = cv.QueryFrame(self.camera)
        self.tk_image = ImageTk.PhotoImage(image = self.resize_image(
            Image.fromstring(RGB, (self.ipl_image.width, 
            self.ipl_image.height), self.ipl_image.tostring(), MODUS, BGR ))) 
        self.config(image = self.tk_image)
        self.root.after(UPDATE_TIME, self.run)
       
    def write_frame(self):
        cv.WriteFrame(self.writer, self.ipl_image)
        
    def resize_image(self, img):
        img_width, img_height = img.size
        if self.height >= self.width:
            div_faktor = (float(str(int(img_width)))
                / float(str(self.width)))
            img_width = img_width / div_faktor
            img_height = img_height / div_faktor
        else:
            div_faktor = (float(str(int(img_height)))
                / float(str(self.height)))
            img_width = img_width / div_faktor
            img_height = img_height / div_faktor
        
        return img.resize((int(img_width), int(img_height)))
        
if __name__ == '__main__':
    root = tk.Tk()
    cam = Cam(root, WIDTH, HEIGHT, CAM, MOVIE_NAME)
    cam.pack()
    def write_image():
       cam.write_frame()
    tk.Button(root, text = BUTTON_TEXT, width = BUTTON_WIDTH,
              command = write_image).pack()
    cam.run()
    root.mainloop()
Gruß Frank