我正在尝试制作一个非常小的 2d 游戏引擎,并且我正在尝试让用户选择一个文件。我正在使用 pygame 编写我的代码,但是要选择一个文件,你需要 tkinter 或制作你自己的文件打开器,我很懒惰。我有两个问题,但我只会在这里写我的第一个问题。我有文件的名称和位置,当我这样做时 img.imwrite("background.jpg")
它给了我一个错误,所以我想也许我会在一个单独的文件中运行它来测试我是否写对了,或者它不起作用,我仍然得到一个错误。
代码:
import cv2
img = cv2.imread("input.jpg")
img.imwrite("output.jpg")
错误: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'
谢谢!
最佳答案
imread
返回图像数据的 numpy 数组。 imwrite(filename,image)
(doc here) 获取名称和图像数据,以指定格式保存。您不能调用imread
好像它是图像数据的一种方法。
关于python-3.x - 属性错误 : 'numpy.ndarray' object has no attribute 'imwrite' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64627848/