7

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
3
  • 3
    new_image_path is a Path…?! Perhaps imwrite requires it to be a string…? Commented Dec 16, 2020 at 12:08
  • 1
    The path in cv2.imwrite cannot be a Path. Wrap your Path object in str() and it shall work. Commented Dec 16, 2020 at 23:05
  • Please past directly from code, rather than trying to retype. Both of those fragments have syntax errors due to missing single quotes. Commented Dec 17, 2020 at 5:54

1 Answer 1

11

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])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.