我必须在Python中加载包含二进制掩码的gif
import numpy as np
from PIL import Image
import imageio
from matplotlib import pyplot as plt
maskPIL = np.array(Image.open('mask.gif'))
maskIO = np.array(imageio.imread('mask.gif'))
plt.subplot(1,2,1)
plt.title('PIL Mask')
plt.imshow(maskPIL,cmap='Greys')
plt.subplot(1,2,2)
plt.title('ImageIO Mask')
plt.imshow(maskIO,cmap='Greys')
plt.show()
为什么这两种方法表现不同?
PIL版本:8.0.1
imageio版本:2.9.0
如果您这样做:
输出
您将看到您的图像是一个调色板图像-因为
mode=P
。这意味着图像中的值不是RGB或灰度值,而是索引到调色板中。如果您查看调色板:您将看到条目0是rgb(255255),因此这意味着无论您的图像中哪里有零,它都应该显示为白色!无论图像中的哪个位置有一个,它都应该显示为黑色
如果您想要正确的值,如灰度,则需要将图像转换为
L
模式,然后所有像素都将是实际的灰度值:更充分的解释here
相关问题 更多 >
编程相关推荐