I need to do this on a Windows computer. Are there any programs that provides such a capability? I will be just as satisfied if I can implement this capability in python. I know in python I can get the (rgb_values) at (xy_position) in (my_image.png) as follows:
xy_position = (100, 100)
img = PIL.Image.open("my_image.png")
rgb_image = img.convert("RGB")
rgb_values = rib_image.getpixel(xy_position)
I am also familiar with tkinter, but haven't found a way to make a python program that will pass the appropriate value of (xy_position) to the code above. Using tkinter isn't required.
I looked at all tkinter widgets and it seems none solve the problem.
CodePudding user response:
Here's a basic app with a working example - you'll probably want to tweak the window geometry
if you're working with images larger than 600x600! You could make it dynamic if you call something like self.geometry(f'{dim_x}x{dim_y 20}')
after declaring dim_x
and dim_y
(the extra 20 pixels on the y axis is to leave room for the label)
import tkinter as tk
from PIL import Image, ImageTk
from pathlib import Path
class App(tk.Tk):
def __init__(self):
super().__init__()
self.geometry=('600x600')
self.img_path = Path(r'C:\Path\To\image.png')
self.img = Image.open(self.img_path)
self.img_rgb = self.img.convert('RGB')
dim_x, dim_y = self.img_rgb.size
self.img_tk = ImageTk.PhotoImage(self.img_rgb.resize((dim_x, dim_y)))
self.canvas = tk.Canvas(self)
self.canvas.create_image(dim_x // 2, dim_y // 2, image=self.img_tk)
self.canvas.pack(expand=True, fill=tk.BOTH)
self.rgb_var = tk.StringVar(self, '0 0 0')
self.rgb_label = tk.Label(self, textvariable=self.rgb_var)
self.rgb_label.pack()
self.bind('<Motion>', lambda e: self.get_rgb(e))
def get_rgb(self, event):
x, y = event.x, event.y
try:
rgb = self.img_rgb.getpixel((x, y))
self.rgb_var.set(rgb)
except IndexError:
pass # ignore errors if the cursor is outside the image
if __name__ == '__main__':
app = App()
app.mainloop()