AttributeError: 'numpy.ndarray' object has no attribute 'imwrite'

Viewed 1530

Im trying to make a very minor 2d game engine, and I am trying to get the user to select a file. I am using pygame to write my code, but to choose a file you would need tkinter or make your own file opener and I am to lazy to. I have two problems but i am only going to write my first one here. I have the name and location of the file and when i do img.imwrite("background.jpg") it gives me an error, so I thought maybe I'll just run it in a seperate file to test if im writing it right, or it doesn't work, and i still get an error.

Code:

import cv2
img = cv2.imread("input.jpg")
img.imwrite("output.jpg")

Error:

Traceback (most recent call last):
  File "/Users/shauryapatel/Desktop/PythonPrograms/GameEngine/player.py", line 3, in <module>
    img.imwrite("output.jpg")
AttributeError: 'numpy.ndarray' object has no attribute 'imwrite'

Thank you!

1 Answers

imread returns a numpy array of the image data. imwrite(filename,image) (doc here) takes a name and image data, saving it in the specified format. You can't call imread as if it were a method of the image data.

Related