元组索引必须是整数,而不是元组

时间:2017-07-30 18:45:57

标签: python tuples

我正在尝试检索图像中的平均RGB值

def DetectColour((x ,y) ,n, image):
    r, g, b = 0, 0, 0
    count = 0
       for s in range(x, x+n+1):
           for t in range(y, y+n+1):
               pixlr, pixlg, pixlb = image[s, t]
               r += pixlr
               g += pixlg
               b += pixlb
               count += 1
    return((r/count), (g/count), (b/count))

我认为此代码中存在问题,但我不知道如何修复它

有问题的错误:

Traceback (most recent call last):
  File "C:\Python27\Sound-o-Colour.py", line 74, in <module>
    r, g, b = DetectColour((25, 25) ,5 ,image) #finds the average colour in the frame
  File "C:\Python27\Sound-o-Colour.py", line 19, in DetectColour
    pixlr, pixlg, pixlb = image[s, t] #Counts the pixels of each colour, red, green and blue
TypeError: tuple indices must be integers, not tuple

1 个答案:

答案 0 :(得分:1)

当你尝试访问列表或元组的成员时,我认为图像是你用sqare括号内的整数来做的,如下所示:

image[s][t]

我想你可能正试图这样做:

image

这将访问{{1}} list / tuple的int(s)成员。 如果这个成员恰好也是一个列表,你可以通过添加另一个方括号来访问它的成员,并在其中指定该成员的索引。 如果你的循环带你通过图像中的像素矩阵,这也是有意义的,因为第一个循环你可能会经过像素行,第二个通过列并尝试检索RGB值。

相关问题