3

I'm trying to write a save image function. The below code is expected to create a copy of the original image as saved-test-image.jpg.

originalImage = "test-image.jpg"
savedImage = cv2.imwrite("saved-test-image.jpg",originalImage)

The execution of the lines gives the follwing back:

Traceback (most recent call last):
  File "UnitTest.py", line 159, in test_save_and_delete_image
    savedImage = cv2.imwrite('unittest-images/saved-test-image.jpg', originalImage)
TypeError: img is not a numpy array, neither a scalar

What needs to be changed here?

1
  • 3
    You should first imread(..) the image. Commented Aug 1, 2017 at 14:37

1 Answer 1

7

Well opencv is right, you save originalImage, but originalImage is a string (the filename, your first line).

You need to cv2.imread(..) your image into a numpy array first:

originalImage = cv2.imread("test-image.jpg")
savedImage = cv2.imwrite("saved-test-image.jpg",originalImage)

If you simply want to copy an image file however, there is no need to load it into memory, you can simply copy the file, without using opencv:

from shutil import copyfile

originalImage = "test-image.jpg"
copyfile(originalImage,"saved-test-image.jpg")

In this case, it will simply copy the file - regardless what its content is - so even if it is a corrupt image, or not an image at all, it will be copied.

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.