当我通过PIL打开tif图像时,有两种情况。第一个是可以打开RGB图像并正常显示的普通图像。但是,第二个代码如下所示:
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)
只能以形状(2040,2040)打开,前一个形状为(2040,2040,3)。当我使用img = img.convert('RGB')
将(2040,2040)转换为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)
答案 0 :(得分:1)
您遇到的问题是因为该图像是PIL图像库不支持的16位图像(uint16)。正如您所建议的,您可以简单地将其转换为其他图像类型。但是,您也可以只使用imageio
以下代码应该起作用:
import imageio
import matplotlib.pyplot as plt
%matplotlib inline
img = imageio.imread('test1.tif')
plt.imshow(img)
请参见此处以了解相关主题:https://github.com/imageio/imageio/issues/204
顺便说一句,这是PIL图像格式以及它们具有多少个通道的列表(改编自https://helpful.knobs-dials.com/index.php/Python_usage_notes_-_PIL):
PIL pixel formats:
RGB 24bits per pixel, 8-bit-per-channel RGB), 3 channels
RGBA (8-bit-per-channel RGBA), 4 channels
RGBa (8-bit-per-channel RGBA, remultiplied alpha), 4 channels
1 - 1bpp, often for masks, 1 channel
L - 8bpp, grayscale, 1 channel
P - 8bpp, paletted, 1 channel
I - 32-bit integers, grayscale, 1 channel
F - 32-bit floats, grayscale, 1 channel
CMYK - 8 bits per channel, 4 channels
YCbCr - 8 bits per channel, 3 channels