Files
@ a1e2cc709d53
Branch filter:
Location: OneEye/exp/color_sampler.py - annotation
a1e2cc709d53
1.4 KiB
text/x-python
photo collector
9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 9df0d1a019c5 | import os
import tkinter as tk
from colorsys import rgb_to_hsv
from PIL import Image,ImageTk
class Sampler:
def __init__(self):
self.dirname="../images"
self.filenames=[f for f in os.listdir(self.dirname) if f.endswith(".jpg")]
self.k=0
self.img=None
self.photo=None
self.letter="_"
def sample(self,e):
(r,g,b)=(x/255 for x in self.img.getpixel((e.x,e.y)))
(h,s,v)=map(lambda x: round(x,3), rgb_to_hsv(r,g,b))
print("\t".join(map(str, (self.filenames[self.k],e.x,e.y,self.letter,h,s,v))))
def showImage(self):
self.img=Image.open(os.path.join(self.dirname,self.filenames[self.k]))
(w,h)=self.img.size
self.photo=ImageTk.PhotoImage(self.img)
self.canvas.create_image(0,0,image=self.photo,anchor="nw")
self.canvas.configure(width=w,height=h)
def switchImage(self,step):
n=len(self.filenames)
self.k=(self.k+step+n)%n
self.showImage()
def setLetter(self,c):
self.letter=c
def createGUI(self):
root=tk.Tk()
frame=tk.Frame(root)
frame.pack()
self.canvas=tk.Canvas(frame)
self.canvas.pack()
self.showImage()
self.canvas.bind('<1>',self.sample)
root.bind("<Left>",lambda e: self.switchImage(-1))
root.bind("<Right>",lambda e: self.switchImage(1))
root.bind("<b>",lambda e: self.setLetter("b"))
root.bind("<e>",lambda e: self.setLetter("e"))
root.bind("<w>",lambda e: self.setLetter("w"))
root.mainloop()
s=Sampler()
s.createGUI()
|