Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # author: Bartlomiej "furas" Burek (https://blog.furas.pl)
- # date: 2020.07.06
- # https://stackoverflow.com/questions/62746177/crop-webcam-live-with-cv2-in-python
- import tkinter as tk
- import cv2
- from PIL import Image, ImageTk
- # --- functions ---
- def show_frame():
- ret, frame = cap.read()
- if frame is not None:
- # crop numpy array
- #frame = frame[0:900, 100:200]
- frame = cv2.flip(frame, 1)
- frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
- image = Image.fromarray(frame)
- # OR crop PIL image
- image = image.crop((100, 0, 200, 900))
- photo = ImageTk.PhotoImage(image=image)
- label_main.photo = photo
- label_main.configure(image=photo)
- label_main.after(10, show_frame)
- # --- main ---
- #width = 1000
- #height = 1000
- cap = cv2.VideoCapture(0)
- #cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
- #cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
- root = tk.Tk()
- #width_px = root.winfo_screenwidth()
- #height_px = root.winfo_screenheight()
- #display = '{}x{}'.format(500, height_px)
- #root.geometry(display)
- #root.configure(background='green')
- root.bind('<Escape>', lambda event:root.destroy())
- label_main = tk.Label(root)
- label_main.pack()
- show_frame()
- root.mainloop()
- cap.release()
Add Comment
Please, Sign In to add comment