How to resize video stream from cv2 according to tkinter window How to resize video stream from cv2 according to tkinter window tkinter tkinter

How to resize video stream from cv2 according to tkinter window


So after reading your code, it seems you are overcomplicating the issue by trying to create an event that resizes it for you.

So I've gone back to basics and just created a webcam stream that will resize whenever you resize the window while maintaining the aspect ratio.

Here is some code that I've found and modified, it doesn't use tkinter, but it's not that important because you can incorporate this in wherever you see fit.

The only major issue I have with this, is when resizing you may find that your window will freeze, but only for a few seconds, it will come back after a while, you wont actually freeze the image, just window control.

You may need to use python multithreading, I actually might try and do that this evening because I find this quite interesting.

import cv2def main():    windowName = "Top"    cv2.namedWindow(windowName)    cap = cv2.VideoCapture(0)    cap.set(3, 650)    cap.set(4, 750)        if cap.isOpened():        ret, frame = cap.read()    else:        ret = False    while ret:        ret, frame = cap.read()        cv2.imshow(windowName, frame)        if cv2.waitKey(1) == 27:            break    cv2.destroyAllWindow()    cap.release()   if __name__== "__main__":    main() 

But in terms of actual functionality, this will let you stream your camera and resize the window without it cropping out, maintaining the same aspect ratio.

if you have any questions let me know