【问题标题】:Python PIL getdata() method doesn't return a tuplePython PIL getdata() 方法不返回元组
【发布时间】:2016-03-28 19:32:51
【问题描述】:

我有一个关于 Python 模块 PIL 的问题:

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

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()) 不返回一个包含元组的列表,而是一个完全由整数组成的列表?

【问题讨论】:

    标签: python image image-processing python-imaging-library


    【解决方案1】:

    如果您打开一个调色板文件(如 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), ...]
    

    【讨论】:

    • 另外,请记住image.convert("RGB") 返回一个图像,它不会改变图像本身。
    猜你喜欢
    • 2019-10-05
    • 1970-01-01
    • 2022-12-12
    • 2019-05-15
    • 2013-02-12
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多