-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui.py
290 lines (232 loc) · 10.7 KB
/
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
from __future__ import print_function
from future import standard_library
standard_library.install_aliases()
from builtins import range
import logging
import tkinter
import tkinter.filedialog
import tkinter.messagebox
import PIL.Image
import PIL.ImageTk
import numpy as np
import random
from pypoi import poissonblending
from pypoi.image_managers import SourceImageManager, DestinationImageManager
from pypoi.util import resource_path as rp
SAVE_MASK_ENABLED = False # Show 'Save mask image' button
logger = logging.getLogger('GUI')
class PoissonBlendingApp(tkinter.Tk):
NUM_OF_EXAMPLES = 50
def __init__(self, parent):
tkinter.Tk.__init__(self, parent)
self.src_img_manager = SourceImageManager()
self.dst_img_manager = DestinationImageManager(self.src_img_manager)
self.parent = parent
self.entry = None
self.label_text = None
self.entry_text = None
self.image_result = None
self.initialize()
def initialize(self):
self.create_menu()
self.create_widgets()
self.src_img_manager.set_path(rp('./images/test1_src.png'))
self.src_img_manager.load(rp('./images/test1_mask.png'))
self.dst_img_manager.set_path(rp('./images/test1_target.png'))
self.dst_img_manager.load()
from pypoi.images.config import offset
self.dst_img_manager.offset = offset[0]
self.src_img_manager.draw()
def create_widgets(self):
self.grid()
# Source and Destination images
label_dst = tkinter.Label(self)
label_dst.grid(row=0, column=0)
self.dst_img_manager.set_tk_label(label_dst)
tkinter.Label(self, text="+").grid(row=0, column=1)
label_src = tkinter.Label(self)
label_src.grid(row=0, column=2)
self.src_img_manager.set_tk_label(label_src)
# Destination image buttons (Move, Rotate)
dst_img_buttons = tkinter.Frame(self)
dst_img_buttons.grid(row=1, column=0)
dst_edit_mode = tkinter.StringVar()
dst_edit_mode.trace('w', self.dst_img_manager.mode_changed)
dst_edit_mode.set('move')
for text, mode in self.dst_img_manager.EDIT_MODES:
b = tkinter.Radiobutton(
dst_img_buttons, text=text, variable=dst_edit_mode, value=mode,
indicatoron=0)
b.pack(side=tkinter.LEFT)
self.dst_img_manager.set_edit_mode_str(dst_edit_mode)
# Source image buttons (Draw/Erase/Move)
src_img_buttons = tkinter.Frame(self)
src_img_buttons.grid(row=1, column=2)
src_edit_mode = tkinter.StringVar()
src_edit_mode.set('draw')
for text, mode in self.src_img_manager.EDIT_MODES:
b = tkinter.Radiobutton(src_img_buttons, text=text,
variable=src_edit_mode, value=mode,
indicatoron=0)
b.pack(side=tkinter.LEFT)
self.src_img_manager.set_edit_mode_str(src_edit_mode)
b = tkinter.Button(src_img_buttons, text='Clear',
command=self.src_img_manager.clear_mask)
b.pack(side=tkinter.LEFT)
# Size buttons
def _draw_size_buttons(row, column, functions):
size_buttons = tkinter.Frame(self)
size_buttons.grid(row=row, column=column)
plus_button = tkinter.Button(size_buttons, text='+', width=2,
command=functions['+'])
original_buttton = tkinter.Button(size_buttons, text='100%',
width=5,
command=functions['original'])
minus_button = tkinter.Button(size_buttons, text='-', width=2,
command=functions['-'])
minus_button.pack(side=tkinter.LEFT)
original_buttton.pack(side=tkinter.LEFT)
plus_button.pack(side=tkinter.LEFT)
# Size buttons for source image
_draw_size_buttons(2, 2, self.src_img_manager.ZOOM_FUNCTIONS)
_draw_size_buttons(2, 0, self.dst_img_manager.ZOOM_FUNCTIONS)
# Blend button
action_frame = tkinter.Frame(self)
action_frame.grid(row=3, column=0, columnspan=3)
if SAVE_MASK_ENABLED:
def save_mask():
self.src_img_manager.save_mask_image()
print(self.dst_img_manager.offset)
tkinter.Button(action_frame, text='Save mask image',
command=save_mask).pack()
tkinter.Button(action_frame, text=u'Show dst mask', command=self.show_dst_mask).pack()
tkinter.Button(action_frame, text=u'Show src mask', command=self.show_src_mask).pack()
tkinter.Button(action_frame, text=u'Generate new mask', command=self.generate_mask).pack()
tkinter.Button(action_frame, text=u'Blend & save', command=self.blend).pack()
tkinter.Button(action_frame, text=u'save pre', command=self.savepre).pack()
def create_menu(self):
menu_bar = tkinter.Menu(self)
file_menu = tkinter.Menu(menu_bar)
file_menu.add_command(label='Open Source Image',
command=self.src_img_manager.open_from_dialog)
file_menu.add_command(label='Open Destination Image',
command=self.dst_img_manager.open_from_dialog)
menu_bar.add_cascade(label="File", menu=file_menu)
run_menu = tkinter.Menu(menu_bar)
run_menu.add_command(label='Blend!', command=self.blend)
menu_bar.add_cascade(label="Run", menu=run_menu)
example_menu = tkinter.Menu(menu_bar)
for i in range(self.NUM_OF_EXAMPLES):
i += 1
example_menu.add_command(label="%d" % i,
command=self.load_example(i))
menu_bar.add_cascade(label="Examples", menu=example_menu)
self.config(menu=menu_bar)
def load_example(self, example_id):
"""
Load an example to GUI.
:param example_id: id starts with *one*
"""
ii = random.randint(0, 50)
example_id += 0
#ii = example_id
if example_id < 10:
example_id = '000' + str(example_id)
elif (example_id >= 10 and example_id < 100):
example_id = '00' + str(example_id)
elif (example_id >= 100 and example_id < 1000):
example_id = '0' + str(example_id)
else:
example_id = str(example_id)
if ii < 10:
ii = '000' + str(ii)
elif (ii >= 10 and ii < 100):
ii = '00' + str(ii)
elif (ii >= 100 and ii < 1000):
ii = '0' + str(ii)
else:
ii = str(ii)
def _load_example():
# src_path = rp('./images/%d.jpg' % example_id)
# dst_path = rp('./images/%d.jpg' % example_id)
# Use absolute paths for src_path, dst_path and mask_path
src_path = ("D://xxx//xxx//pypoi//images/image/"+example_id+".jpg")
dst_path = ("D://xxx//xxx//pypoi//images//image/"+ii+".jpg")
mask_path = None
try:
from pypoi.images.config import offset
self.dst_img_manager.offset=(0,0)#self.dst_img_manager.offset = offset[example_id - 1]
self.dst_img_manager.rotate = 0
mask_path = ("D://xxx//xxx//pypoi//images//mask//"+example_id+".jpg")
except IndexError:
pass
self.src_img_manager.open(src_path, mask_path=mask_path)
self.dst_img_manager.open(dst_path)
return _load_example
def blend(self):
angle = self.dst_img_manager.rotate
src = np.array(self.src_img_manager.image_src.rotate(angle))
dst = np.array(self.dst_img_manager.image)
mask = np.array(self.src_img_manager.image_mask.rotate(angle))
# poissonblending.blend takes (y, x) as offset,
# whereas gui has (x, y) as offset values so reverse these values.
reversed_offset = self.dst_img_manager.offset[::-1]
global app
app.title(reversed_offset)
blended_image = poissonblending.blend(dst, src, mask, reversed_offset)
self.image_result = PIL.Image.fromarray(np.uint8(blended_image))
self.image_tk_result = PIL.ImageTk.PhotoImage(self.image_result)
result_window = tkinter.Toplevel()
label = tkinter.Label(result_window, image=self.image_tk_result)
label.image = self.image_tk_result # for holding reference counter
label.pack()
save_button = tkinter.Button(result_window, text='Save',
command=self.save_result(result_window))
save_button.pack()
result_window.title("Blended Result")
self.save_all(self.save_result(result_window))
def show_dst_mask(self):
mask = self.dst_img_manager.draw_full_mask()
mask.show()
return
def savepre(self):
import os
_, filename = os.path.split(self.src_img_manager.path)
image = self.dst_img_manager.imagepre()
image.save('images/imagepre/' + filename)
def show_src_mask(self):
self.src_img_manager.image_mask.show()
def generate_mask(self):
self.src_img_manager.image_src.save('matting/data/image/tmp.png')
self.src_img_manager.image_mask.save('matting/data/trimap/tmp.png')
from matting import deploy
deploy.foo()
self.src_img_manager.image_mask = PIL.Image.open('matting/data/pred/tmp.png').convert("L")
self.src_img_manager.draw()
def save_all(self, save_result):
import os, shutil
_, filename = os.path.split(self.src_img_manager.path)
dst_mask = self.dst_img_manager.draw_full_mask()
shutil.copy('matting/data/image/tmp.png', 'images/origin/'+filename)
shutil.copy('matting/data/trimap/tmp.png', 'images/trimap/'+filename)
dst_mask.save('images/mask/'+filename)
save_result('images/blended/'+filename)
def save_result(self, parent, file_name=None):
def _save_result(file_name):
if not file_name:
file_name = tkinter.filedialog.asksaveasfilename(parent=parent)
try:
self.image_result.save(file_name)
except KeyError:
msg = 'Unknown extension. Supported extensions:\n'
msg += ' '.join(list(PIL.Image.EXTENSION.keys()))
tkinter.messagebox.showerror("Error", msg)
return _save_result
def main():
global app
logging.basicConfig(level=logging.INFO)
app = PoissonBlendingApp(None)
app.title('PyPoi: "Py"thon Program for "Poi"sson Image Editing')
app.mainloop()
if __name__ == "__main__":
main()