首页 > 解决方案 > 制作图像的副本

问题描述

我应该为我的 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:“元组”对象没有属性“副本”

标签: python

解决方案


鉴于您必须放入img=image.copy()函数,最简单的方法应该是翻转np_img = np.array(image)前一行的顺序。我假设image在我们在这里看到的内容之前,您的论点尚未转换为 numpy 数组。

之后,您应该更改img=image.copy()为适当的变量。

也就是说,我认为最好在执行任何其他操作之前立即将图像加载为 numpy 矩阵。这样,您可以在任何功能之前制作副本,从而降低每个功能的成本。


推荐阅读