Pixel RGB Value Identifier - TypeError: argument must be sequence of length 2

Pixel RGB Value Identifier - TypeError: argument must be sequence of length 2

我一直在使用 PIL 时遇到问题并试图让我的程序打印像素的 rgb 值。

代码:

import PIL

try:
    import Image
except ImportError:
    from PIL import Image

###
    
filename = input ('Picture Filename - ')

image = PIL.Image.open(filename)

image.show()

width, height = image.size

image_rgb = image.convert('RGB')

w = 1
h = 1

a = 1
b = 2
while a < b:
    while (w <= width):
        rgb_pixel_value_1 = int(image_rgb.getpixel(w))
        rgb_pixel_value_2 = int(image_rgb.getpixel(h))
        print(rgb_pixel_value_1)
        print(rgb_pixel_value_2)
        w += 1
    if w == width and h < height:
        h += 1
    if h == height:
        a = 3

预期输出: 程序应该打印从 [1, 1] 到 [height, width]

每个像素的值

实际输出:

Traceback (most recent call last):
  File "I:/Programming/Python/Useful Projects/Image Colour Space Checker/Revision 1.py", line 27, in <module>
    rgb_pixel_value_1 = int(image_rgb.getpixel(w))
  File "C:\Users\rdmor\AppData\Roaming\Python\Python38\site-packages\PIL\Image.py", line 1367, in getpixel
    return self.im.getpixel(xy)
TypeError: argument must be sequence of length 2

我尝试过的: 我尝试的很少,因为互联网上关于这个主题的内容太少了,所有相关的东西充其量只是有点用

可能还有其他错误,但我还没有注意到这些错误

从指定的 x 和 y 获取像素时,getpixel 需要传递长度为 2 的序列。在你的代码中你有

rgb_pixel_value_1 = int(image_rgb.getpixel(w))

从你需要的指定位置获取rgb值

r, g, b = image_rgb.getpixel((w, h))