Skip to main content
 首页 » 编程设计

python之枕头 Image.convert TypeError : argument 1 must be str, 不是 int

2024年11月01日14youxin

我正在尝试从这个 repo 运行一个模型.并收到以下错误:

Traceback (most recent call last): 
  File "denet_glaucoma_screen/Demo_DENet_GlaucomaScreen.py", line 72, in <module> 
    org_img = np.array(Image.fromarray(org_img).resize((2048, int(org_img.shape[1] * img_scale))).convert(3)) 
  File "/usr/local/lib/python3.6/dist-packages/PIL/Image.py", line 975, in convert 
    im = self.im.convert(mode, dither) 
TypeError: argument 1 must be str, not int 

我相信给出错误的部分是这样的:

    for lineIdx, file_test in enumerate(file_test_list): 
        temp_txt = [elt.strip() for elt in file_test.split(',')] 
        org_img = np.asarray(image.load_img(os.path.join(data_img_path,  
    temp_txt[0]))) 
 
    img_scale = 2048.0 / org_img.shape[0] 
    org_img = np.array(Image.fromarray(org_img).resize((2048, int(org_img.shape[1] * img_scale))).convert(3)) 
 

查看Image.convert我不明白为什么那里会有 3

我试着把3改成"3","RGB",甚至把整个convert code> 关闭,但我遇到了各种错误,例如 unable to process that type of data 或类似的错误。

  • 更新 1:

我尝试更改行: org_img = np.array(Image.fromarray(org_img).resize((2048, int(org_img.shape[1] * img_scale))).convert(3))
到
org_img = np.array(Image.fromarray(org_img).resize((2048, int(org_img.shape[1] *img_scale), 3)))
但是我得到了以下错误:

   File "denet_glaucoma_screen/Demo_DENet_GlaucomaScreen.py", line 74, in <module> 
    img_scale), 3))) 
  File "/usr/local/lib/python3.6/dist-packages/PIL/Image.py", line 1745, in resize 
    return self._new(self.im.resize(size, resample, box)) 
TypeError: argument 1 must be sequence of length 2, not 3 
  • 更新 2: 尝试将以上内容更改为: org_img = np.array(Image.fromarray(org_img).resize((2048, int(org_img.shape[1] * img_scale), 3)), mode='RGB')
    但是我收到以下错误:
  File "denet_glaucoma_screen/Demo_DENet_GlaucomaScreen.py", line 74, in <module> 
    img_scale), 3)), mode='RGB') 
  File "/usr/local/lib/python3.6/dist-packages/PIL/Image.py", line 1745, in resize 
    return self._new(self.im.resize(size, resample, box)) 
TypeError: argument 1 must be sequence of length 2, not 3 
  • 更新 3: 试运行:
org_img = np.array(Image.fromarray(org_img, mode='RGB').resize((2048, int(org_img.shape[1]*img_scale), 3))) 

但是我得到了以下错误:

  File "denet_glaucoma_screen/Demo_DENet_GlaucomaScreen.py", line 73, in <module> 
    org_img = np.array(Image.fromarray(org_img, mode='RGB').resize((2048, int(org_img.shape[1]*img_scale), 3))) 
  File "/usr/local/lib/python3.6/dist-packages/PIL/Image.py", line 1745, in resize 
    return self._new(self.im.resize(size, resample, box)) 
TypeError: argument 1 must be sequence of length 2, not 3 

请您参考如下方法:

您错误地更改了原始存储库中的代码。这是他们为 org_img 准备的:

org_img = scipy.misc.imresize(org_img, (2048, int(org_img.shape[1]*img_scale), 3)) 

scipy 中的 imresize 已贬值。如果您查看旧的 scipy 函数的代码和文档,您可以看到它们正在为数组传递 org_img 和 (2048, int(org_img. shape[1]*img_scale), 3) 用于大小 - 即 3 是指定调整大小的元组的一部分 - 他们没有像你一样将它用作转换的 mode试图在你的代码中做。

因为这是在 3D 中,根据调整大小的 3 个维度,scipy 文档说:

For 3-D and 4-D arrays, mode will be set to 'RGB' and 'RGBA' respectively.

所以你想要:

numpy.array(Image.fromarray(org_img, mode='RGB').resize((2048, int(org_img.shape[1]*img_scale))))