'cv2.error: OpenCV(4.5.2) 👎 error: (-5:Bad argument) in function 'cvtColor'

this is my code and I have a problem. What is the solution for it, please? I try to make a screen recorder:

import numpy as np
import cv2
import pyautogui

codec = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter("Recorded.avi", codec, 60, (1366,768))
cv2.namedWindow("Recording", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Recording", 480, 270)

while True:
     img = pyautogui.screenshot #capturing screenshot
     frame = np.array(img) # converting the image into numpy array representation
     frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # converting the BGR image into RGB image
     out.write(frame) # writing the RBG image to file
     cv2.imshow('Recording', frame) # display screen/frame being recorded
     if cv2.waitKey(1) == ord('q'): # Wait for the user to press 'q' key to stop the recording
          break

 out.release() # closing the video file
 cv2.destroyAllWindows() # destroying the recording window

The problem here:

Traceback (most recent call last): File "C:\Users\mhmdj\PycharmProjects\learn\main.py", line 13, in frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # converting the BGR image into RGB image cv2.error: OpenCV(4.5.2) 👎 error: (-5:Bad argument) in function 'cvtColor'

Overload resolution failed:

src data type = 17 is not supported Expected Ptrcv::UMat for argument 'src'



Solution 1:[1]

You could try loading the image with cv2.imread() rather than the np.array() to see if that works since the examples use this method:

img = cv2.imread(imagePath)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anders Elmgren