tkinter getRGB()image参数究竟应该是什么?(Python)

2024-09-30 10:39:27 发布

您现在位置: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)

是我现在的全部。运行此返回

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,用pic替换image,但没什么区别。图像可以是像我这样的文件路径吗?在


Tags: 文件inpy图像image程序getvalue
1条回答
网友
1楼 · 发布于 2024-09-30 10:39:27

image必须是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)

相关问题 更多 >

    热门问题