Last active
April 26, 2024 08:46
-
-
Save Bertz-TII/2b518169569d15b8609d53d9c6b64582 to your computer and use it in GitHub Desktop.
DirectShow + TKinter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# requires PyGrabber (https://github.com/andreaschiavinato/python_grabber) | |
from pygrabber.PyGrabber import PyGrabber | |
from pygrabber import dshow_graph | |
from tkinter import Tk, Message, ttk | |
def on_closing(event=0): | |
print("stop") | |
grabber.stop() | |
try: | |
root.destroy() | |
except Exception as e: | |
print(e) | |
def updateWinsize(event): | |
# grabber.update_window(event.width, event.height) | |
setPos_centered(event.width, event.height) | |
def grabbedFrame(*args): | |
print(args) | |
def setPos_centered(width, height): | |
img_w, img_h = grabber.graph.filters[dshow_graph.FilterType.video_input].get_current_format() | |
scale_w = width / img_w | |
scale_h = height / img_h | |
scale = min(scale_w, scale_h, 1) | |
w = int(img_w * scale) | |
h = int(img_h * scale) | |
dx = (width - w) / 2 | |
dy = (height - h) / 2 | |
grabber.graph.filters[dshow_graph.FilterType.render].set_window_position(int(dx), int(dy), w, h) | |
grabber = PyGrabber(grabbedFrame) | |
grabber.set_device(0) | |
root = Tk() | |
root.geometry("400x400") | |
root.grid_columnconfigure(0, weight=1) | |
root.grid_rowconfigure(1, weight=1) | |
top = ttk.Frame(root) | |
top.grid(row=0, column=0) | |
ttk.Button(top, text="Info", command=lambda: grabber.set_device_properties()).grid(row=0, column=0) | |
ttk.Button(top, text="Grab", command=lambda: grabber.grab_frame()).grid(row=0, column=1) | |
bottom = ttk.Frame(root) | |
bottom.grid(row=1, column=0, sticky='nsew') | |
id = bottom.winfo_id() | |
grabber.start_preview(id) | |
root.protocol("WM_DELETE_WINDOW", on_closing) | |
bottom.bind("<Configure>", updateWinsize) | |
if __name__ == '__main__': | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment