0

I'm making a program to print out the color values of pixels in an image and referencing chenlian's answer to a question here to read RGB values. I isolated this part of the program into a different file to test it.

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)

is all i have right now. running this 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'

What is image supposed to be exactly? I've tried replacing the words image with my pic variable in both spots and pic with image but it makes no difference. Can image be a filepath like what I have?

Community
  • 1
  • 1
dioretsa
  • 65
  • 1
  • 2
  • 9

1 Answers1

1

image has to be an instance of PhotoImage, not the file name 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)
  • It runs fine, but every time i run it, it tells me the values are all 255 regardless of my specified pixel – dioretsa Oct 26 '15 at 21:19
  • Is there a lot of white in the picture? –  Oct 27 '15 at 21:20
  • the picture is 4 pixels, one completely red, one completely green, one completely blue, and one completely white in a square shape – dioretsa Oct 31 '15 at 17:05
  • It is impossible to tell what is happening until you post the new code that you are using, but your original question has been answered, the argument sent to the function is a PhotoImage instance, not a string, so you should start a new thread with a new question and the new code. –  Oct 31 '15 at 17:52