Python PIL open tif图像只有一个通道

Python PIL open tif图像只有一个通道,python,numpy,python-imaging-library,tiff,pillow,Python,Numpy,Python Imaging Library,Tiff,Pillow,当我通过PIL打开tif图像时,有两种情况。第一个是一个普通的,可以打开一个图像作为RGB和显示plt以及。但是,第二个代码如下所示: from PIL import Image import numpy as np import matplotlib.pyplot as plt img = Image.open('test1.tif') a = np.array(img) a.shape # output: (2040,2040) 只能以(20402040)的形状打开,前一个形状为(2040

当我通过PIL打开tif图像时,有两种情况。第一个是一个普通的,可以打开一个图像作为RGB和显示plt以及。但是,第二个代码如下所示:

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
img = Image.open('test1.tif')
a = np.array(img)
a.shape
# output: (2040,2040)
只能以(20402040)的形状打开,前一个形状为(20402040,3)。当我使用
img=img.convert('RGB')
将(20402040)转换为RGB模式并通过plt显示时,只看到一个空白方块

拜托,有人,告诉我怎么了? 提前谢谢

原始图像:

和numpy阵列:

array([[7256, 6926, 7270, ..., 7368, 7457, 7555],
   [7174, 6945, 6878, ..., 7401, 7353, 7895],
   [7288, 7099, 7140, ..., 7359, 7524, 7587],
   ...,
   [6769, 6695, 6698, ..., 6599, 6788, 6898],
   [6780, 6683, 6857, ..., 6723, 6761, 6861],
   [6788, 6626, 6761, ..., 6738, 6751, 7054]], dtype=uint16)
array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)
转换为RGB模式后: numpy阵列:

array([[7256, 6926, 7270, ..., 7368, 7457, 7555],
   [7174, 6945, 6878, ..., 7401, 7353, 7895],
   [7288, 7099, 7140, ..., 7359, 7524, 7587],
   ...,
   [6769, 6695, 6698, ..., 6599, 6788, 6898],
   [6780, 6683, 6857, ..., 6723, 6761, 6861],
   [6788, 6626, 6761, ..., 6738, 6751, 7054]], dtype=uint16)
array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)

您遇到的问题是,该图像是PIL图像库不支持的16位图像(uint16)。正如您所建议的,您可以简单地将其转换为不同的图像类型。但是,您也可以只使用
imageio

以下代码应该可以工作:

import imageio
import matplotlib.pyplot as plt
%matplotlib inline

img = imageio.imread('test1.tif')
plt.imshow(img)
有关相关主题的讨论,请参见此处:

顺便说一句,这里列出了PIL图像格式以及它们有多少个频道(改编自):


你能打印a[0][0]的值或a的值的其他样本吗?@Zev是的,我可以全部打印出来。在转换为RGB模式之前,该值为1000+,在所有值均为255之后,您可以共享该图像吗?@Zev I通过添加图像更新了问题您没有提到您使用的特定matplotlib函数。您是否尝试过plt.imshow()?