使用 Pillow 提取像素值 - 我使用的每个坐标都给出相同的值

Extracting pixel values using Pillow - every coordinate I use gives the same value

我正在尝试使用 Python 2.7 中的 Pillow 库来提取 PNG 和 JPG 图像上给定坐标处的像素值。我没有收到错误,但无论我在值确实不同的图像上使用的坐标如何,我总是得到相同的值。

这是我打印所有值的脚本摘录(它是一个小测试图像):

from PIL import Image

box = Image.open("col_grad.jpg")

pixels = list(box.getdata())

print(pixels)

从我尝试提取单个值开始:

from PIL import Image, ImageFilter

box = Image.open("col_grad.jpg")

value = box.load()

print(value[10,10])

我一直在使用关于此主题的这些先前问题作为指导,包括:

Getting list of pixel values from PIL

How can I read the RGB value of a given pixel in Python?

感谢您对此的任何帮助,

亚历克斯

您应该考虑执行以下操作之一:

首先将您的图像转换为灰度,然后找到存在的像素值。

img_grey= img.convert('1')     # convert image to black and white

SECOND 如果你想要RGB通道的像素值,你必须分割你的彩色图像。然后在特定坐标处找到所有通道中的像素值。

Image.split(img) 

由于图像数据的复杂性,我不确定您是否可以按照您想要的方式访问它。

就得到the pixel:

Image.getpixel(xy)

Returns the pixel value at a given position.
Parameters:   xy – The coordinate, given as (x, y).
Returns:  The pixel value. If the image is a multi-layer image, this method returns a tuple.: