python - 制作图像的副本
问题描述
我应该为我的 python 程序创建几个函数,每个程序都需要我处理输入图像的副本。因此,我需要为代码中的每个函数编写 img = image.copy() 。但是,当我运行代码时,我返回一个 AttributeError 说“'tuple' 对象没有属性 'copy'。”
鉴于我仍然必须在函数中的某处包含语句 img = image.copy(),我该如何更改我的代码以消除此错误?在使用 copy() 之前,我是否需要先将图像更改为 numpy 数组?
代码:
def func(image):
img = image.copy() #error code appeared here
np_img = np.array(image)
rsize, csize = len(img), len(img[0]) #denoting the rows and columns of pixels of the image respectively
(the rest of the code)
错误消息:AttributeError:“元组”对象没有属性“副本”
解决方案
鉴于您必须放入img=image.copy()
函数,最简单的方法应该是翻转np_img = np.array(image)
前一行的顺序。我假设image
在我们在这里看到的内容之前,您的论点尚未转换为 numpy 数组。
之后,您应该更改img=image.copy()
为适当的变量。
也就是说,我认为最好在执行任何其他操作之前立即将图像加载为 numpy 矩阵。这样,您可以在任何功能之前制作副本,从而降低每个功能的成本。
推荐阅读
- java - ClassNotFoundException 与 NoClassDefFoundError 与无法找到或加载主类 XYZ 有什么区别?
- python - python - 从元组中提取第一个元素
- c++ - 命名空间“std”没有成员“string_view”
- javascript - 如何在页面加载之前首先在函数内部运行 httprequest?
- python-3.x - python中没有名为“pip”的模块
- css - Angular 为 div 添加边距
- php - WooCommerce:在 FlexSlider 画廊导航中更改图像版本
- python - 如何使用 Python 在 Autentique API 中上传文件?
- html - 在按钮和输入中将文本与内容框的底部对齐
- javascript - 如何在节点中使用导出的 api 端点并表达