Python PIL getdata() 方法没有 return 元组

Python PIL getdata() method doesn't return a tuple

我对 Python 模块 PIL 有疑问:

每当我在图像上调用 getdata() 方法时,我都会得到一些奇怪的东西 returned。

from PIL import Image

# Histogram class to get the data
class Histogram:
    def __init__(self, image):
        image.convert("RGB")
        pixel_value_list = list(image.getdata())
        print(pixel_value_list[1])

image = Image.open("lenna.gif")
histogram = Histogram(image)

但我没有将元组打印到控制台,但不知何故 45...

为什么 list(image.getdata()) 不是 return 一个包含元组的列表,而是一个完全由整数组成的列表?

如果您打开调色板文件(如 GIF),并通过 .getdata() 打印像素列表,您将获得调色板中的索引列表,例如:

im = Image.open("composplot.gif")
print(list(im.getdata()))

输出:

[0, 0, 0, 0, 0, 0, 0, 0, 6, 223, 0, 0, 46, 219, 195, ...]

但是,如果将调色图像转换为 RGB 图像,您将获得 (r,g,b) 元组列表。示例:

im = Image.open("composplot.gif")
imrgb = im.convert("RGB")
print(list(imrgb.getdata()))

输出:

[(255, 255, 255), (255, 255, 255), (216, 216, 216), (8, 8, 8), (191, 191, 191), (255, 255, 255), ...]