-
Notifications
You must be signed in to change notification settings - Fork 0
/
droplets_gui.py
347 lines (296 loc) · 12.3 KB
/
droplets_gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import string
import tkinter
import string
import tkinter
from tkinter import *
from tkinter import colorchooser
from PIL import Image
from PIL import ImageTk
from PIL import ImageColor
from tkinter import filedialog
from tkinter import ttk
import numpy as np
import cv2
import os
import time
import http.server
import threading
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
import os
import time
import http.server
import threading
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
import socket
import serial
from serial.tools import list_ports
class MyServer(threading.Thread):
def __init__(self, IP_ADDR):
threading.Thread.__init__(self)
self.ip_addr = IP_ADDR
def run(self):
self.server = ThreadingHTTPServer((self.ip_addr, 8888), SimpleHTTPRequestHandler)
self.server.serve_forever()
def stop(self):
self.server.shutdown()
class GUI:
def __init__(self):
self.ip_addr = socket.gethostbyname(socket.gethostname())
self.ip_addr = socket.gethostbyname(socket.gethostname())
self.filename = None
self.directory = None
self.directory = None
self.filename_closest = None
self.filename_txt = None
self.src = None
self.src_cv2 = None
self.port = None
self.root = Tk()
self.image_size = (10,10) #default 10x10
self.pix_list = []
self.color_set = ['#FFFFFF', '#000000'] #default Black and White
self.root.title("Image Loader")
self.root.geometry("750x1000")
self.root.geometry("750x1000")
self.root.resizable(width = True, height = True)
self.root.configure(bg='white')
self.root.configure(bg='white')
main_btn = Button(self.root, text = 'open image', command=lambda: self.open_img()).grid(row = 1)
#main_btn = Button(self.root, text = 'open image', command=lambda: self.open_img()).place(x = 0, y =250)
frame_size = ttk.Frame(self.root, padding = (32)).grid()
label_width = ttk.Label(frame_size, text = 'width', padding = (5,2)).grid(row = 3, column = 0, sticky = E)
#label_width = ttk.Label(frame_size, text = 'width', padding = (5,2), width = 8).place(x = 0, y = 275)
label_height = ttk.Label(frame_size, text = 'height', padding = (5,2)).grid(row = 4, column = 0, sticky = E)
#label_height = ttk.Label(frame_size, text = 'height', padding = (5,2), width = 8).place(x = 0, y = 300)
label_color = ttk.Label(frame_size, text = 'color set', padding = (5,2)).grid(row = 5, column = 0, sticky = E)
#label_color = ttk.Label(frame_size, text = 'color set', padding = (5,2), width = 8).place(x = 0, y = 325)
width = StringVar()
width_entry = ttk.Entry(frame_size, textvariable=width, width=10).grid(row = 3, column = 1)
#width_entry = ttk.Entry(frame_size, textvariable=width, width=10).place(x = 80, y = 275)
height = StringVar()
height_entry = ttk.Entry(frame_size, textvariable=height, width=10).grid(row = 4, column = 1)
#height_entry = ttk.Entry(frame_size, textvariable=height, width=10).place(x = 80, y = 300)
color = StringVar()
color_entry = ttk.Entry(frame_size, textvariable=color, width=10).grid(row = 5, column = 1)
#color_entry = ttk.Entry(frame_size, textvariable=color, width=10).place(x = 80, y = 325)
set_size_btn = Button(self.root, text = 'OK', command=lambda: self.set_size(width, height, color)).grid(row = 6)
#set_size_btn = Button(self.root, text = 'OK', command=lambda: self.set_size(width, height, color)).place(x = 200, y = 300)
run_btn = Button(self.root, text = 'run', command = lambda: self.transformation(self.image_size, color)).grid(row = 8)
run_btn = Button(self.root, text = 'run', command = lambda: self.transformation(self.image_size, color)).grid(row = 8)
#run_btn = Button(self.root, text = 'run', command = lambda: self.transformation(self.image_size, color)).place(x = 200, y = 325)
dev = [info.device for info in list_ports.comports()]
self.port = StringVar()
select_box = ttk.Combobox(self.root, textvariable=self.port, values=dev, style='office.TCombobox').grid(row = 9)
send_btn = Button(self.root, text = 'send to Arduino', command = lambda: self.Serial_Com()).grid(row = 9)
send_btn = Button(self.root, text = 'send to Arduino', command = lambda: self.Serial_Com()).grid(row = 10)
# send_btn = Button(self.root, text = 'send to Arduino', command = lambda: self.send_to_Arduino()).grid(row = 10)
# send_btn = Button(self.root, text = 'send to Arduino', command = lambda: self.send_to_Arduino()).place(x = 0, y = 605)
frame = ttk.Frame(self.root).grid()
ip_txt = ttk.Entry(frame, width = 30)
ip_txt.insert(END, "http://" + self.ip_addr + ":8888/pix.txt")
#ip_txt.place(x = 0, y = 630)
ip_txt.grid(row = 11)
# disp_btn = Button(self.root, text="display IP address", command=lambda: self.dispaly_ip()).place(x = 0, y = 620)
self.root.mainloop()
def open_img(self):
# Select the Imagename from a folder
self.filename = self.openfilename()
# open the image and display
img = Image.open(self.filename)
self.src = img
img = img.resize((200, 200), Image.Resampling.LANCZOS)
# PhotoImage class is used to add image to widgets, icons etc
img = ImageTk.PhotoImage(img)
# create a label
panel = Label(self.root, image = img)
# set the image as img
panel.image = img
panel.grid(row = 1)
panel.grid(row = 1)
def openfilename(self):
filename = filedialog.askopenfilename(title = "original")
return filename
def transformation(self, size, color_set):
resized_img = self.resize_image(size)
resized_img = resized_img.resize((250, 250), Image.Resampling.LANCZOS) # so that user can see (super blurred)
resized_img = ImageTk.PhotoImage(resized_img)
panel = Label(self.root, image = resized_img)
panel.image = resized_img
panel.grid(row = 7, column = 0)
# panel.place(x = 0, y = 350, column = 0)
# panel.place(x = 0, y = 350)
cv2_closest_resized_img = self.get_closest_image(self.src_cv2, self.color_set)
closest_resized_img = Image.open(self.filename_closest)
closest_resized_img = closest_resized_img.resize((250, 250), Image.Resampling.LANCZOS)
closest_resized_img = ImageTk.PhotoImage(image = closest_resized_img)
panel2 = Label(self.root, image = closest_resized_img)
panel2.image = closest_resized_img
panel2.grid(row = 7, column = 1)
# panel2.place(x = 300, y = 350)
panel2.grid(row = 7, column = 1)
# panel2.place(x = 300, y = 350)
self.convert_jpeg_to_pix(self.filename_closest, self.color_set)
self.output_format(self.pix_list)
def resize_image(self, size):
if self.src is None:
self.filename = self.openfilename()
self.src = Image.open(self.filename)
r_im = self.src.resize((size))
file = self.filename.split('.')
filename = file[0] + '_resized.png'
r_im.save(filename)
self.src_cv2 = cv2.imread(filename)
return r_im
def set_size(self, w, h, c):
self.image_size = (int(w.get()),int(h.get()))
if (len(c.get()) != 0):
self.color_set = (c.get()).split(', ')
def send_to_Arduino(self):
os.chdir(self.directory)
server_address = ('', 8080)
s = MyServer(self.ip_addr)
s.start()
print('thread alive:', s.is_alive()) # True
time.sleep(30)
s.stop()
print('thread alive:', s.is_alive()) # False
def Serial_Com(self):
ser = serial.Serial(self.port.get(), 9600)
print(self.filename_txt)
f = open(self.filename_txt, 'r', encoding='utf_8')
while True:
line = f.readline()
if line:
ser.write(line.encode('utf-8'))
time.sleep(0.5)
time.sleep(0.5)
else:
ser.close()
break
f.close()
def get_closest_color(self, pix, color):
closest_color = None
min_diff = 100000
pix = np.array(pix)
for clr in color:
clr = np.array(clr)
diff = np.sum((clr-pix)**2)
if diff < min_diff:
min_diff = diff
closest_color = clr
return closest_color
def get_closest_image(self, im, color_set):
#im = cv2.imread(filename)
color_set_rgb = [ImageColor.getrgb(color) for color in color_set]
h, w, c = im.shape
im_out = np.zeros((h,w,c))
for i in range(h):
for j in range(w):
b,g,r = im[i,j]
c_r, c_g, c_b = self.get_closest_color((r,g,b), color_set_rgb)
im_out[i,j,:] = (c_b, c_g, c_r)
# fixing the order of rgb
file = self.filename.split('.')
self.filename_closest = file[0] + '_closest.png'
cv2.imwrite(self.filename_closest, im_out)
return im_out
def convert_jpeg_to_pix(self, filename, color_set):
im = Image.open(filename, 'r')
w, h = im.size
im = im.convert('RGB')
color_set_rgb = [ImageColor.getrgb(color) for color in color_set]
self.pix_list.clear()
self.pix_list.clear()
if w%2==0:
for i in range(h):
for j in range(w):
if i%2==0:
pix = color_set_rgb.index(im.getpixel((j,i)))
else:
pix = color_set_rgb.index(im.getpixel((w-1-j,i)))
self.pix_list.append(pix)
else:
for i in range(h):
for j in range(w):
if i%2==0:
pix = color_set_rgb.index(im.getpixel((w-1-j,i)))
else:
pix = color_set_rgb.index(im.getpixel((j,i)))
self.pix_list.append((pix))
def output_format(self, pix):
file = self.filename.split('/')
path = ''
for dr in file:
if '.' in dr:
continue
else:
path += dr + '/'
self.directory = path
self.filename_txt = self.directory + 'pix.txt'
txt = open(self.filename_txt, 'w').close()
file = self.filename.split('/')
path = ''
for dr in file:
if '.' in dr:
continue
else:
path += dr + '/'
self.directory = path
self.filename_txt = self.directory + 'pix.txt'
txt = open(self.filename_txt, 'w').close()
txt = open(self.filename_txt, 'a')
np.savetxt(self.filename_txt, pix, fmt='%d', delimiter=" ")
txt.close()
return
# def dispaly_ip(self):
# self.ip_txt.insert(END, "http://" + self.ip_addr + ":8888/pix.txt")
# def main(IP_ADDR):
# test = GUI(IP_ADDR)
#
# if __name__ == '__main__':
# main(sys.argv[1] if len(sys.argv) > 1 else "")
if __name__ == '__main__':
test = GUI()
#need to implement menu tab
#need to implement select menu for serial port -> done
# README first -> instructions
# repo name: drop to image
# add title, background, centered
# making small changes to make sure mirror worked
##
# color wheel
# Entry box to IP Address
# link to image
# link to files
# fix sets of images
##
# pictures from David to make it more clear w/ OpenCV
# compare the original image and one from Arduino
# photoshop filter
# % of match, which one is wrong
# frame shift or one drop
# picture of flow
# idealized final version and what we have right now
# combine ok and run
# GUI like lite brite??
# add button to add more than 1 image to show
# README first -> instructions
# repo name: drop to image
# add title, background, centered
# making small changes to make sure mirror worked
##
# color wheel
# Entry box to IP Address
# link to image
# link to files
# fix sets of images
##
# pictures from David to make it more clear w/ OpenCV
# compare the original image and one from Arduino
# photoshop filter
# % of match, which one is wrong
# frame shift or one drop
# picture of flow
# idealized final version and what we have right now
# combine ok and run
# GUI like lite brite??