Can't save videos that are cropped

Hi all. I am making a screen recorder with added functionality of being able to only record a section of the screen, (cropping realtime).

I’m stumped on why the write() function is not saving.
this is my code section:

img = pyautogui.screenshot()
frame = np.array(img)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
cropped = frame[500:1000, 500:1000]  
out.write(cropped)
cv2.imshow('LIVE', cropped)

i’ve trouble shooted it down to just the write function I think. imshow() is working fine. screen recorder also works fine without cropping.

thanks for any help

MRE is required. out is not defined. also watch that you have a waitKey() somewhere, or else the imshow window will remain numb.

OpenCV is for computer vision, not for video encoding. look at the imageio package or the PyAV package.

VideoWriter expects you to give it frames that are the exact same size you promised in the constructor.

wow that did it. thank you. I just needed to match the dimensions with the cropped dimensions for the numpy array to write out. although it appears it has to be squared dimensions.

it does not. you probably put width and height in the wrong order somewhere. OpenCV wants (W, H) tuples for any cv::Size() argument, not numpy shape tuples.