使用 PIL 和 Imageio 正确加载二进制掩码/GIF

问题描述

我必须在 Python 中加载一个包含二进制掩码的 gif。

inputmask

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)
plt.title('ImageIO Mask')
plt.imshow(maskIO,cmap='Greys')

plt.show()

Result

为什么这两种方法的行为不同?

PIL 版本:8.0.1

imageio 版本:2.9.0

解决方法

如果你这样做:

im = Image.open('mask.gif')
print(im)

输出

<PIL.GifImagePlugin.GifImageFile image mode=P size=683x512 at 0x7FC0C86FF430>

您会看到您的图像是一个调色板图像 - 因为 mode=P。这意味着图像中的值不是 RGB 或灰度值,而是调色板的索引。如果您查看调色板:

np.array(im.getpalette()).reshape(256,3)
Out[25]: 
array([[255,255,255],<--- palette entry 0
       [  0,0],<--- palette entry 1
       [  2,2,2],[  3,3,3],[  4,4,4],[  5,5,5],...
       ...

您将看到条目 0 是 rgb(255,255),这意味着您的图像中只要有零,它就应该显示为白色!并且无论您的图像中有任何位置,它都应显示为黑色。

如果您想要适当的值,如灰度,您需要将图像转换为 L 模式,那么您的所有像素都将是实际的灰度值:

maskPIL = np.array(Image.open('mask.gif').convert('L'))

更完整的解释here