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
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)
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.
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.
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With