Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #import scipy
- #import numpy as np
- from Tkinter import *
- import tkFileDialog
- from PIL import Image as IM
- import ImageTk
- import cPickle as pickle
- import array
- global color_dict
- global original_image
- global our_image
- # This dictionary will hold all our image data
- color_dict = {}
- color_dict["orange"] = []
- color_dict["yellow"] = []
- color_dict["cyan"] = []
- color_dict["green"] = []
- color_dict["white"] = []
- color_dict["robot_blue"] = []
- color_dict["robot_pink"] = []
- class _Colortable_Generator:
- def __init__(self, master):
- self.initComplete = 0
- frame = Frame(master, width=900, height=600)
- frame.pack()
- self.master = master
- self.x, self.y, self.w, self.h = -1,-1,-1,-1
- self.master.bind('<Enter>', self.bindConfigure)
- # This stuff all generates the main GUI itself
- self.master.title("Colortable_Generator")
- self.Button_3 = Button(self.master,text="Save Colors", width="15")
- self.Button_3.place(x=24, y=468, width=117, height=28)
- self.Button_3.bind("<ButtonRelease-1>", self.Button_3_Click)
- self.Button_4 = Button(self.master,text="Load Colors", width="15")
- self.Button_4.place(x=24, y=516, width=116, height=27)
- self.Button_4.bind("<ButtonRelease-1>", self.Button_4_Click)
- self.Button_5 = Button(self.master,text="Quit", width="15")
- self.Button_5.place(x=24, y=566, width=116, height=27)
- self.Button_5.bind("<ButtonRelease-1>", quit)
- self.Button_6 = Button(self.master,text="Write Look-Up Table", width="15")
- self.Button_6.place(x=424, y=540, width=120, height=27)
- self.Button_6.bind("<ButtonRelease-1>", self.Button_6_Click)
- self.Clear_Select_Button = Button(self.master,text="Clear Selected Colors", width="15")
- self.Clear_Select_Button.place(x=24, y=360, width=127, height=28)
- self.Clear_Select_Button.bind("<ButtonRelease-1>", self.Clear_Select_Button_Click)
- self.Load_Montage_Button = Button(self.master,text="Load Image", width="15")
- self.Load_Montage_Button.place(x=24, y=36, width=119, height=30)
- self.Load_Montage_Button.bind("<ButtonRelease-1>", self.Load_Montage_Button_Click)
- lbframe = Frame( self.master )
- self.Canvas_1_frame = lbframe
- scrollbar = Scrollbar(lbframe, orient=VERTICAL)
- self.Canvas_1 = Canvas(lbframe, width="640", background="white", height="480", yscrollcommand=scrollbar.set)
- scrollbar.config(command=self.Canvas_1.yview)
- scrollbar.pack(side=RIGHT, fill=Y)
- self.Canvas_1.pack(side=LEFT, fill=BOTH, expand=1)
- self.Canvas_1_frame.place(x=180, y=36)
- self.Canvas_1.bind("<ButtonRelease-1>", self.Canvas_1_Click)
- self.Threshold_Val_Entry = Entry(self.master,width="15")
- self.Threshold_Val_Entry.place(x=60, y=432, width=40, height=22)
- self.Threshold_Val_Entry_StringVar = StringVar()
- self.Threshold_Val_Entry.configure(textvariable=self.Threshold_Val_Entry_StringVar)
- self.Threshold_Val_Entry_StringVar.set("0")
- self.Threshold_Val_Entry_StringVar_traceName = self.Threshold_Val_Entry_StringVar.trace_variable("w", self.Threshold_Val_Entry_StringVar_Callback)
- self.Label_1 = Label(self.master,text="Threshold", width="15")
- self.Label_1.place(x=24, y=408, width=112, height=22)
- self.Radiobutton_1 = Radiobutton(self.master,text="Orange", value="orange", width="15")
- self.Radiobutton_1.place(x=12, y=84, width=134, height=26)
- self.RadioGroup1_StringVar = StringVar()
- self.RadioGroup1_StringVar.set("orange")
- self.RadioGroup1_StringVar_traceName = self.RadioGroup1_StringVar.trace_variable("w", self.RadioGroup1_StringVar_Callback)
- self.Radiobutton_1.configure(variable=self.RadioGroup1_StringVar )
- self.Radiobutton_2 = Radiobutton(self.master,text="Yellow", value="yellow", width="15")
- self.Radiobutton_2.place(x=12, y=120, width=134, height=26)
- self.Radiobutton_2.configure(variable=self.RadioGroup1_StringVar )
- self.Radiobutton_3 = Radiobutton(self.master,text="Cyan", value="cyan", width="15")
- self.Radiobutton_3.place(x=12, y=156, width=136, height=26)
- self.Radiobutton_3.configure(variable=self.RadioGroup1_StringVar )
- self.Radiobutton_4 = Radiobutton(self.master,text="Field (Green)", value="green", width="15")
- self.Radiobutton_4.place(x=12, y=192, width=140, height=27)
- self.Radiobutton_4.configure(variable=self.RadioGroup1_StringVar )
- self.Radiobutton_5 = Radiobutton(self.master,text="White", value="white", width="15")
- self.Radiobutton_5.place(x=12, y=228, width=134, height=26)
- self.Radiobutton_5.configure(variable=self.RadioGroup1_StringVar )
- self.Radiobutton_6 = Radiobutton(self.master,text="Robot Blue", value="robot_blue", width="15")
- self.Radiobutton_6.place(x=12, y=264, width=134, height=26)
- self.Radiobutton_6.configure(variable=self.RadioGroup1_StringVar )
- self.Radiobutton_7 = Radiobutton(self.master,text="Robot Pink", value="robot_pink", width="15")
- self.Radiobutton_7.place(x=12, y=300, width=134, height=26)
- self.Radiobutton_7.configure(variable=self.RadioGroup1_StringVar )
- self.master.resizable(0,0)
- # The following two functions are tkinter boilerplate
- def bindConfigure(self, event):
- if not self.initComplete:
- self.master.bind("<Configure>", self.Master_Configure)
- self.initComplete = 1
- def Master_Configure(self, event):
- pass
- if event.widget != self.master:
- if self.w != -1:
- return
- x = int(self.master.winfo_x())
- y = int(self.master.winfo_y())
- w = int(self.master.winfo_width())
- h = int(self.master.winfo_height())
- if (self.x, self.y, self.w, self.h) == (-1,-1,-1,-1):
- self.x, self.y, self.w, self.h = x,y,w,h
- if self.w!=w or self.h!=h:
- self.w=w
- self.h=h
- def Button_3_Click(self, event): #click method for component ID=15
- pass
- # This uses pickle to save our color dict to a file
- save_file = file("Saved_Colortable", "a")
- pickle.dump(color_dict,save_file)
- save_file.close()
- def Button_6_Click(self, event): #click method for component ID=15
- pass
- look_up_table = [0]*262143 # our LUT must have 262143 spots for the 262143 possible color combos
- # Run the following on each color_dict color:
- orange_list = color_dict["orange"]
- yellow_list = color_dict["yellow"]
- cyan_list = color_dict["cyan"]
- green_list = color_dict["green"]
- white_list = color_dict["white"]
- blue_list = color_dict["robot_blue"]
- pink_list = color_dict["robot_pink"]
- y = ()
- u = ()
- v = ()
- for i in range(len(orange_list)):
- color_triple = orange_list[i] # This is 3 decimal YUV values in the form (Y,U,V)
- y = bin(color_triple[0])
- y = y[:-2]
- u = bin(color_triple[1])
- u = u[:-2]
- v = bin(color_triple[2])
- v = v[:-2]
- yuv_space = y + u[2:] + v[2:]
- yuv_space_decimal = int(yuv_space,2)
- offset = 0
- while offset < int(self.Threshold_Val_Entry_StringVar.get()):
- look_up_table[yuv_space_decimal - offset] = 1 # "orange"
- look_up_table[yuv_space_decimal + offset] = 1 # "orange"
- offset += 1
- offset = 0
- for i in range(len(yellow_list)):
- color_triple = yellow_list[i] # This is 3 decimal YUV values in the form (Y,U,V)
- y = bin(color_triple[0])
- y = y[:-2]
- u = bin(color_triple[1])
- u = u[:-2]
- v = bin(color_triple[2])
- v = v[:-2]
- yuv_space = y + u[2:] + v[2:]
- yuv_space_decimal = int(yuv_space,2)
- offset = 0
- while offset < int(self.Threshold_Val_Entry_StringVar.get()):
- look_up_table[yuv_space_decimal - offset] = 2 #"yellow"
- look_up_table[yuv_space_decimal + offset] = 2 #"yellow"
- offset += 1
- offset = 0
- for i in range(len(cyan_list)):
- color_triple = cyan_list[i] # This is 3 decimal YUV values in the form (Y,U,V)
- y = bin(color_triple[0])
- y = y[:-2]
- u = bin(color_triple[1])
- u = u[:-2]
- v = bin(color_triple[2])
- v = v[:-2]
- yuv_space = y + u[2:] + v[2:]
- yuv_space_decimal = int(yuv_space,2)
- offset = 0
- while offset < int(self.Threshold_Val_Entry_StringVar.get()):
- look_up_table[yuv_space_decimal - offset] = 4 #"cyan"
- look_up_table[yuv_space_decimal + offset] = 4 #"cyan"
- offset += 1
- offset = 0
- for i in range(len(green_list)):
- color_triple = green_list[i] # This is 3 decimal YUV values in the form (Y,U,V)
- y = bin(color_triple[0])
- y = y[:-2]
- u = bin(color_triple[1])
- u = u[:-2]
- v = bin(color_triple[2])
- v = v[:-2]
- yuv_space = y + u[2:] + v[2:]
- yuv_space_decimal = int(yuv_space,2)
- offset = 0
- while offset < int(self.Threshold_Val_Entry_StringVar.get()):
- look_up_table[yuv_space_decimal - offset] = 8 #"green"
- look_up_table[yuv_space_decimal + offset] = 8 #"green"
- offset += 1
- offset = 0
- for i in range(len(white_list)):
- color_triple = white_list[i] # This is 3 decimal YUV values in the form (Y,U,V)
- y = bin(color_triple[0])
- y = y[:-2]
- u = bin(color_triple[1])
- u = u[:-2]
- v = bin(color_triple[2])
- v = v[:-2]
- yuv_space = y + u[2:] + v[2:]
- yuv_space_decimal = int(yuv_space,2)
- offset = 0
- while offset < int(self.Threshold_Val_Entry_StringVar.get()):
- look_up_table[yuv_space_decimal - offset] = 16 #"white"
- look_up_table[yuv_space_decimal + offset] = 16 #"white"
- offset += 1
- offset = 0
- #for i in range(len(blue_list)):
- # color_triple = blue_list[i] # This is 3 decimal YUV values in the form (Y,U,V)
- # y = bin(color_triple[0])
- # y = y[:-2]
- # u = bin(color_triple[1])
- # u = u[:-2]
- # v = bin(color_triple[2])
- # v = v[:-2]
- # yuv_space = y + u[2:] + v[2:]
- # yuv_space_decimal = int(yuv_space,2)
- # offset = 0
- # while offset < int(self.Threshold_Val_Entry_StringVar.get()):
- #look_up_table[yuv_space_decimal - offset] = "blue"
- #look_up_table[yuv_space_decimal + offset] = "blue"
- # offset += 1
- offset = 0
- #for i in range(len(pink_list)):
- # color_triple = pink_list[i] # This is 3 decimal YUV values in the form (Y,U,V)
- # y = bin(color_triple[0])
- # y = y[:-2]
- # u = bin(color_triple[1])
- # u = u[:-2]
- # v = bin(color_triple[2])
- # v = v[:-2]
- # yuv_space = y + u[2:] + v[2:]
- # yuv_space_decimal = int(yuv_space,2)
- # offset = 0
- # while offset <= int(self.Threshold_Val_Entry_StringVar.get()):
- #look_up_table[yuv_space_decimal - offset] = "pink"
- #look_up_table[yuv_space_decimal + offset] = "pink"
- # offset += 1
- offset = 0
- montage_file = file("Look_Up_Table.raw", "wb")
- final_array = array.array('B')
- final_array.fromlist(look_up_table)
- final_array.tofile(montage_file)
- # Bit-write the whole thing
- def Button_4_Click(self, event): #click method for component ID=16
- pass
- # Button to open saved data
- global color_dict
- try:
- save_file = open("Saved_Colortable", "a")
- color_dict = pickle.load(save_file)
- save_file.close()
- except:
- "File Not Found!"
- def Clear_Select_Button_Click(self, event): #click method for component ID=10
- pass
- color_dict["orange"] = []
- color_dict["yellow"] = []
- color_dict["cyan"] = []
- color_dict["green"] = []
- color_dict["white"] = []
- color_dict["robot_blue"] = []
- color_dict["robot_pink"] = []
- our_photo = ImageTk.PhotoImage(original_image)
- self.Canvas_1.create_image((325,240),image=our_photo)
- # DOESN'T CLEAR CANVAS, SO IMAGE MUST BE RELOADED.
- def Load_Montage_Button_Click(self, event): #click method for component ID=1
- # The montage name is a carryover from when I was still thinking I'd open montage files instead of
- # regular bitmaps
- global our_image
- global original_image
- pass
- filename = tkFileDialog.askopenfilename(filetypes = [('Bitmap Images', '.bmp')])
- our_image = IM.open(filename)
- original_image = our_image
- self.new_image = our_image
- self.our_photo = ImageTk.PhotoImage(self.new_image)
- self.Canvas_1.create_image((325,240),image=self.our_photo)
- def Canvas_1_Click(self, event): #click method for component ID=9
- x = int(event.x)
- y = int(event.y)
- self.AddYuyvToDict(x, y, str(self.RadioGroup1_StringVar.get()))
- # Tkinter boilerplate
- def Threshold_Val_Entry_StringVar_Callback(self, varName, index, mode):
- pass
- def RadioGroup1_StringVar_Callback(self, varName, index, mode):
- pass
- def AddYuyvToDict(self,x_val, y_val, color_label):
- global color_dict
- our_image.load()
- RGB_pixel_triplet = our_image.getpixel((x_val,y_val))
- putpixel = our_image.putpixel
- # putpixel((x_val,y_val),(255,140,0))
- place_x = 0
- place_y = 0
- while place_x < 640:
- while place_y < 480:
- if our_image.getpixel((place_x,place_y)) == RGB_pixel_triplet:
- if self.RadioGroup1_StringVar.get() == "orange":
- putpixel((place_x,place_y),(255,140,0))
- elif self.RadioGroup1_StringVar.get() == "yellow":
- putpixel((place_x,place_y),(255,255,0))
- elif self.RadioGroup1_StringVar.get() == "cyan":
- putpixel((place_x,place_y),(0,255,255))
- elif self.RadioGroup1_StringVar.get() == "green":
- putpixel((place_x,place_y),(34,139,34))
- elif self.RadioGroup1_StringVar.get() == "white":
- putpixel((place_x,place_y),(255,255,255))
- elif self.RadioGroup1_StringVar.get() == "robot_blue":
- putpixel((place_x,place_y),(70,130,180))
- elif self.RadioGroup1_StringVar.get() == "robot_pink":
- putpixel((place_x,place_y),(255,192,203))
- place_y += 1
- place_y = 0
- place_x += 1
- self.new_image = our_image
- self.our_photo = ImageTk.PhotoImage(self.new_image)
- self.Canvas_1.create_image((325,240),image=self.our_photo)
- # Conversion is based on lua_rgb_to_yuyv() function from luaImageProc.cpp
- YUV_Y = RGB_pixel_triplet[1] # same as "uint8_t y = g;"
- YUV_U = 128 + ((RGB_pixel_triplet[2] - RGB_pixel_triplet[1]) / 2)# same as "uint8_t u = 128 + (b-g)/2;"
- YUV_V = 128 + ((RGB_pixel_triplet[0] - RGB_pixel_triplet[1]) / 2)# same as "uint8_t v = 128 + (r-g)/2;"
- # Alternative conversion:
- #YUV_Y = RGB_pixel_triplet[0] * .299000 + RGB_pixel_triplet[1] * .587000 + RGB_pixel_triplet[2] * .114000
- #YUV_U = RGB_pixel_triplet[0] * -.168736 + RGB_pixel_triplet[1] * -.331264 + RGB_pixel_triplet[2] * .500000 + 128
- #YUV_V = RGB_pixel_triplet[0] * .500000 + RGB_pixel_triplet[1] * -.418688 + RGB_pixel_triplet[2] * -.081312 + 128
- color_dict[color_label] += [(YUV_Y, YUV_U, YUV_V)]
- def main():
- root = Tk()
- app = _Colortable_Generator(root)
- root.mainloop()
- if __name__ == '__main__':
- #AddYuyvToDict(1, 1, "orange", [[1,2,3],[1,2,3],[1,2,3]])
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement