cvtColor() without imgread()

Hello, I have been trying to convert an image to grey scale (negative) using this code in python:

def toGrey(self, image, id):
        image = cv2.GaussianBlur(image, (3, 3), 0)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        grad_x = cv2.Sobel(gray, cv2.CV_16S, 1, 0, ksize=3, scale=1, delta=0, borderType=cv2.BORDER_DEFAULT)
        grad_y = cv2.Sobel(gray, cv2.CV_16S, 0, 1, ksize=3, scale=1, delta=0, borderType=cv2.BORDER_DEFAULT)
        abs_grad_x = cv2.convertScaleAbs(grad_x)
        abs_grad_y = cv2.convertScaleAbs(grad_y)
        grad = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
        tmp_path = "storage/"+str(id)+".png"
        cv2.imwrite(tmp_path, grad)
        return cv2.imread(tmp_path)

It works fine with:

puzzle = cv2.imdecode(self.stringToImage(self.puzzle), cv2.IMREAD_COLOR)
puzzle = self.toGrey(puzzle, str('2')
puzzleGray = cv2.cvtColor(puzzle, cv2.COLOR_BGR2GRAY)

def stringToImage(self, base64_string):
        imgdata = base64.b64decode(base64_string)
        return np.frombuffer(imgdata, dtype="uint8")

but I don’t want to save the temporary file, I want it to work without imgread(). It would be great if someone can help me on this, thanks!

looks like you don’t see the forest for all those trees …

just return the grad matrix from your function ?

you would also not need the
puzzleGray = cv2.cvtColor(puzzle, cv2.COLOR_BGR2GRAY) line, then

(it also does not make any sense to write/read the image to disk …)

It worked, thanks alot!