tkinter getRGB() 图像参数到底应该是什么? (python)

what exactly is the tkinter getRGB() image argument supposed to be? (python)

我正在编写一个程序来打印图像中像素的颜色值并引用 chenlian's answer to a question here 来读取 RGB 值。我将这部分程序隔离到不同的文件中进行测试。

pic = "H://fourpxtest.png" from tkinter import * def getRGB(image, x, y): value = image.get(x, y) return tuple(map(int, value.split(" "))) getRGB(pic, 1, 1)

我现在只有这些了。 运行这个returns

Traceback (most recent call last): File "H:/tzrtst.py", line 6, in <module> getRGB(pic, 1, 1) File "H:/tzrtst.py", line 4, in getRGB value = image.get(x, y) AttributeError: 'str' object has no attribute 'get'

image 究竟应该是什么?我已经尝试在两个位置用我的 pic 变量替换单词 image 并且用 image 替换 pic 但这没有区别。图像可以是像我这样的文件路径吗?

图像必须是 PhotoImage 的实例,而不是文件名 http://effbot.org/tkinterbook/photoimage.htm

import sys
if sys.version_info[0] < 3:
    import Tkinter as tk     ## Python 2.x
else:
    import tkinter as tk     ## Python 3.x

grape_gif='''\
R0lGODlhIAAgALMAAAAAAAAAgHCAkC6LV76+vvXeswD/ANzc3DLNMubm+v/6zS9PT6Ai8P8A////
/////yH5BAEAAAkALAAAAAAgACAAAAS00MlJq7046803AF3ofAYYfh8GIEvpoUZcmtOKAO5rLMva
0rYVKqX5IEq3XDAZo1GGiOhw5rtJc09cVGo7orYwYtYo3d4+DBxJWuSCAQ30+vNTGcxnOIARj3eT
YhJDQ3woDGl7foNiKBV7aYeEkHEignKFkk4ciYaImJqbkZ+PjZUjaJOElKanqJyRrJyZgSKkokOs
NYa2q7mcirC5I5FofsK6hcHHgsSgx4a9yzXK0rrV19gRADs=
'''

def getRGB(image, x, y):
    value = image.get(x, y)
    return value


master=tk.Tk()
master.geometry("300x500")

## uses data= instead of file= because the picture
## is data within this program
photo=tk.PhotoImage(data=grape_gif)
print getRGB(photo, 10, 10)