Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cv2.imwrite() SystemError: <built-in function imwrite> returned NULL without setting an error

Tags:

python

cv2

I'm tring to save an np array as an image. The problem is that, if I write the path in the imwrite function it works, but if i store it in a variable and then use this variable as path it doesn't work and returns an error.

This works:

cv2.imwrite('my/path/to/image.png', myarray[...,::-1])

This doesn't work

new_image_path = path/'{}+.png'.format(np.random.randint(1000))
cv2.imwrite(new_image_path, myarray[...,::-1])

And it returns this error:

SystemError: <built-in function imwrite> returned NULL without setting an error
like image 568
Sergio García Avatar asked Dec 16 '20 12:12

Sergio García


People also ask

What is Imwrite in cv2?

cv2. imwrite() method is used to save an image to any storage device. This will save the image according to the specified format in current working directory. Syntax: cv2.imwrite(filename, image)

What does cv2 Imwrite return?

imwrite() returns a boolean value. True if the image is successfully written and False if the image is not written successfully to the local path specified.

What does Imwrite return?

The imwrite() function returns the Boolean value true upon successfully writing or saving the image to the local file system. The imwrite() function returns the Boolean value False upon failing to write the or save the image to the local file system.


1 Answers

The method cv2.imwrite does not work with the Path object. Simply convert it to string when you call the method:

cv2.imwrite(str(new_image_path), myarray[...,::-1])
like image 125
Ophir S Avatar answered Sep 29 '22 13:09

Ophir S