Binary-Cam

Stellt hier eure Projekte vor.
Internetseiten, Skripte, und alles andere bzgl. Python.
Antworten
Benutzeravatar
kaytec
User
Beiträge: 608
Registriert: Dienstag 13. Februar 2007, 21:57

Hallo BlackJack,

läuft bei mir nur so

Code: Alles auswählen

#! /usr/bin/env python
# -*- coding: utf-8
from __future__ import absolute_import, division, print_function
import Tkinter as tk
 
import cv2
from PIL import Image, ImageTk
 
CAPTURE_WIDTH = 640
CAPTURE_HEIGHT = 480    
DEFAULT_CAM_ID = -1
 
 
class CamUI(tk.Label):
 
    UPDATE_INTERVAL = 10
    PROPID_WIDTH = 3
    PROPID_HEIGHT = 4
   
    def __init__(self, parent, width, height, cam_id):
        tk.Label.__init__(self, parent)
        self.parent = parent
        self.width = width
        self.height = height
        self.after_id = None
        self.image = None
        self.cam = cv2.VideoCapture(cam_id)
       
    def start_stop(self):
        if self.after_id is None:
            self.run()
        else:
            self.after_cancel(self.after_id)
            self.after_id = None
       
    def run(self):
        if self.cam.isOpened():
            self.image = ImageTk.PhotoImage(
                image=self.resize_image(
                    Image.fromstring(
                        'L',
                        (
                            int(self.cam.get(self.PROPID_WIDTH)),
                            int(self.cam.get(self.PROPID_HEIGHT))
                        ),
                        cv2.blur(
                            cv2.adaptiveThreshold(
                                cv2.cvtColor(
                                    self.cam.read()[1], cv2.COLOR_BGR2GRAY
                                ),
                                255,
                                cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                cv2.THRESH_BINARY,
                                63,
                                5,
                            ),
                            (5, 5)),
                        'raw',
                    )
                )
            )
            self.config(image=self.image)
        else:
            self.config(width=20, height=5, text='NO CAM', font='Arial 20')
 
        self.after_id = self.after(self.UPDATE_INTERVAL, self.run)
   
    def resize_image(self, img):
        img_width, img_height = img.size
        if self.height >= self.width:
            img_height = img_height * self.width // img_width
            img_width = self.width
        else:
            img_width = img_width * self.height // img_height
            img_height = self.height
        return img.resize((img_width, img_height))
       
    def release(self):
        self.cam.release()
        self.parent.destroy()
 
def main():
    root = tk.Tk()
    root.title('BINARY-CAM')
    cam_ui = CamUI(root, CAPTURE_WIDTH, CAPTURE_HEIGHT, DEFAULT_CAM_ID)
    cam_ui.pack()
    tk.Button(text='Start/Stop', command=cam_ui.start_stop).pack()
    cam_ui.run()
    root.protocol("WM_DELETE_WINDOW", cam_ui.release)
    root.mainloop()
    
if __name__ == '__main__':
    main()
Gruß
Benutzeravatar
kaytec
User
Beiträge: 608
Registriert: Dienstag 13. Februar 2007, 21:57

Hallo,
jetzt ist es eine Warhol-Cam.

Code: Alles auswählen

#! /usr/bin/env python
# -*- coding: utf-8
from __future__ import absolute_import, division, print_function
import Tkinter as tk
 
import cv2
from PIL import Image, ImageTk, ImageOps
 
PICTURE_COLORS_2_3 = ((("#F7FF00", "#2E64FE"), 
                       ("#000000", "#00FF40"), 
                       ("#CD00FF", "#FF8000")), 
                      (("#FF0000", "#B700FF"), 
                       ("#FF00AF", "#FFFF00"), 
                       ("#31B404", "#FE2E2E")))
                       
CAPTURE_WIDTH = 640
CAPTURE_HEIGHT = 350
DEFAULT_CAM_ID = -1
 
class CamUI(tk.Label):
 
    UPDATE_INTERVAL = 10
    PROPID_WIDTH = 3
    PROPID_HEIGHT = 4
                        
    def __init__(self, parent, width, height, cam_id, picture_colors):
        tk.Label.__init__(self, parent)
        self.parent = parent
        self.width = width
        self.height = height
        self.after_id = None
        self.image = None
        self.picture_colors = picture_colors
        self.cam = cv2.VideoCapture(cam_id)
       
    def start_stop(self):
        if self.after_id is None:
            self.run()
        else:
            self.after_cancel(self.after_id)
            self.after_id = None
       
    def run(self):
        if self.cam.isOpened():
            image = Image.fromstring(
                    'L',
                    (
                        int(self.cam.get(self.PROPID_WIDTH)),
                        int(self.cam.get(self.PROPID_HEIGHT))
                    ),
                    cv2.blur(
                        cv2.adaptiveThreshold(
                            cv2.cvtColor(
                                self.cam.read()[1], cv2.COLOR_BGR2GRAY
                            ),
                            255,
                            cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                            cv2.THRESH_BINARY,
                            63,
                            5,
                        ),
                        (5, 5)),
                    'raw',
                )
                    
            self.image = ImageTk.PhotoImage(self.multiply_image(image))
            self.config(image=self.image)
        else:
            self.config(width=20, height=5, text='NO CAM', font='Arial 20')
            
        self.after_id = self.after(self.UPDATE_INTERVAL, self.run)
        
    def multiply_image(self, image):
        img_width, img_height = image.size
        warhol_img_width = img_width * len(self.picture_colors[0])
        warhol_img_height = img_height * len(self.picture_colors)
        warhol_image = Image.new("RGB", (warhol_img_width, warhol_img_height))
        for x, row in enumerate(self.picture_colors):
            for y, colors in enumerate(row):
                warhol_image.paste(ImageOps.colorize(image, colors[0],
                    colors[1]), (img_width * y, img_height * x))
        return self.resize_image(warhol_image)
   
    def resize_image(self, img):
        img_width, img_height = img.size
        if self.height >= self.width:
            img_height = img_height * self.width // img_width
            img_width = self.width
        else:
            img_width = img_width * self.height // img_height
            img_height = self.height
        return img.resize((img_width, img_height))
       
    def release(self):
        self.cam.release()
        self.parent.destroy()
 
def main():
    root = tk.Tk()
    root.title('WARHOL-CAM')
    cam_ui = CamUI(root, CAPTURE_WIDTH, CAPTURE_HEIGHT, DEFAULT_CAM_ID, 
        PICTURE_COLORS_2_3)
    cam_ui.pack()
    tk.Button(text='Start/Stop', command=cam_ui.start_stop).pack()
    cam_ui.run()
    root.protocol("WM_DELETE_WINDOW", cam_ui.release)
    root.mainloop()
    
if __name__ == '__main__':
    main()
Gruß Frank
Antworten