Pillow Image.convert TypeError:参数 1 必须是 str,而不是 int



我正在尝试从此存储库运行模型,并收到以下错误:

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,但我遇到了各种错误,如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))

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

由于这是 3D 的,根据调整大小的 3 个维度,scipy文档说:

对于 3-D 和 4-D 阵列,模式将分别设置为"RGB"和"RGBA"。

所以你想要:

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

相关内容

最新更新