我需要将PNG H*W*4rgba图像转换为rgb形状为H*W*3的图像。在
我可以这样做,但是当我保存它时,图像会再次保存为H*W*4
以下是代码片段:for idx, image in enumerate(image_names):
#matplotlib as mpi here I use plt for plotting and mpi for read
rgba = mpi.imread(os.path.join(read_path,image))
#convert to rgb using skimage.color as rtl,
rgb = rtl.rgba2rgb(rgba)
#change path of the image to be saved
resized_path = os.path.join(os.path.sep,Ims,p[0],image)
print(np.shape(rgb))#shape is printed (136,136,3)
mpi.imsave(resized_path,rgb)
在这之后,当我再次阅读它时,它的形状又是H*W*4知道为什么吗?我猜matplotlib imsave有什么问题吗?在
参考图像:
编辑
更新代码如下:
^{pr2}$
出现以下错误:ValueError Traceback (most recent call last)
in ()
6 print(np.shape(rgb))
7 rgb = Image.fromarray((rgb*255).astype(np.uint8))
----> 8 rgb.save(resized_path)
9 #mpi.imsave(resized_path,rgb)
/usr/local/lib/python2.7/dist-packages/PIL/Image.pyc in save(self, fp, format, **params)
1809 format = EXTENSION[ext]
1810 except KeyError:
-> 1811 raise ValueError('unknown file extension: {}'.format(ext))
1812
1813 if format.upper() not in SAVE:
ValueError: unknown file extension:
解决方案
下面的答案是正确的上面唯一的问题是调整了路径的大小,下面是更改:resized_path = os.path.join(os.path.sep,Ims,p[0],image)
resized_path = (resized_path+".png")