-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIControl.py
252 lines (199 loc) · 8.75 KB
/
UIControl.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
import os
import wx
from wx.lib.pubsub import pub
import wx.lib.statbmp as SB
from PIL import Image
PhotoMaxSize = 800
maxSize = 600
pub.sendMessage("state", message = 'input')
# drop module from https://www.blog.pythonlibrary.org/2017/10/25/wx_drag_and_drop_images/
class DropTarget(wx.FileDropTarget):
def __init__(self, widget):
wx.FileDropTarget.__init__(self)
self.widget = widget
def OnDropFiles(self, x, y, filenames):
image = Image.open(filenames[0])
image.thumbnail((PhotoMaxSize,PhotoMaxSize))
image.save('thumbnail.png')
pub.sendMessage('dnd', filepath='thumbnail.png')
return True
#Input frame
class Input(wx.Frame):
#create input frame and set background color
def __init__(self):
self.frame = wx.Frame(None, title='Anima', size = (1200, 600))
self.panel = wx.Panel(self.frame)
self.panel.SetBackgroundColour(wx.Colour(233,243,255,255))
pub.subscribe(self.update_image_on_dnd, 'dnd')
self.createWidgets()
self.frame.Show(),
# create components for the input frame
def createWidgets(self):
instructions = 'Browse for an image or Drag and Drop'
#create placeholder to let the user drop their image -- https://www.blog.pythonlibrary.org/2017/10/25/wx_drag_and_drop_images/
img = wx.Image(800,600)
self.imageCtrl = SB.GenStaticBitmap(self.panel, wx.ID_ANY,
wx.Bitmap(img))
filedroptarget = DropTarget(self)
self.imageCtrl.SetDropTarget(filedroptarget)
#Set title and font styles
title = "Anima"
lbl = wx.StaticText(self.panel,-1,style = wx.ALIGN_CENTER)
font = wx.Font(48, wx.FONTFAMILY_MODERN, wx.NORMAL, wx.NORMAL)
subFont = wx.Font(15, wx.FONTFAMILY_MODERN, wx.NORMAL, wx.NORMAL)
lbl.SetFont(font)
lbl.SetLabel(title)
instructLbl = wx.StaticText(self.panel, label=instructions)
instructLbl.SetFont(subFont)
self.style = 'shinkai'
#Create drop-down menu
choices = ['shinkai', 'hayao', 'hosoda', 'paprika']
self.choice = wx.Choice(self.panel,choices = choices)
self.choice.Bind(wx.EVT_CHOICE, self.onChoice)
#Create button to let the user browse images from local disk -- https://www.blog.pythonlibrary.org/2017/10/25/wx_drag_and_drop_images/
self.photoTxt = wx.TextCtrl(self.panel, size=(300,-1))
browseBtn = wx.Button(self.panel, label='Browse')
browseBtn.Bind(wx.EVT_BUTTON, self.on_browse)
#create run button
runButton = wx.Button(self.panel, label='Run')
runButton.Bind(wx.EVT_BUTTON, self.onRun)
#use sizer to organize the layout
self.mainSizer = wx.BoxSizer(wx.VERTICAL)
self.sizer = wx.BoxSizer(wx.HORIZONTAL)
self.output = wx.BoxSizer(wx.VERTICAL)
self.mainSizer.Add(lbl, 0, wx.CENTER, 5)
self.mainSizer.Add(self.choice, 0, wx.CENTER, 5)
self.mainSizer.Add(wx.StaticLine(self.panel, wx.ID_ANY),
0, wx.ALL|wx.EXPAND, 5)
self.mainSizer.Add(instructLbl, 0, wx.ALL, 5)
self.mainSizer.Add(self.imageCtrl, 0, wx.LEFT|wx.RIGHT, 100)
self.sizer.Add(self.photoTxt, 0, wx.ALL, 5)
self.sizer.Add(browseBtn, 0, wx.ALL, 5)
self.mainSizer.Add(self.sizer, 0, wx.CENTER, 5)
self.mainSizer.Add(runButton, 0, wx.ALL|wx.CENTER, 20)
self.panel.SetSizer(self.mainSizer)
self.mainSizer.Fit(self.frame)
self.panel.Layout()
def on_browse(self, event):
# browse image from local disk -- https://www.blog.pythonlibrary.org/2017/10/25/wx_drag_and_drop_images/
wildcard = "JPEG files (*.jpg)|*.jpg"
dialog = wx.FileDialog(None, "Choose a file",
wildcard=wildcard,
style=wx.FD_OPEN)
if dialog.ShowModal() == wx.ID_OK:
self.photoTxt.SetValue(dialog.GetPath())
dialog.Destroy()
self.on_view()
def onRun(self, event):
# send message through pubsub
pub.sendMessage("state", message = 'output')
# run the backend code
os.system('python3 driver.py --input ' + self.pathDir + ' --styles ' + self.style + ' --overwrite')
# send message through pubsub and close current frame
pub.sendMessage("path", message = self.pathDir, arg2 = self.style)
self.frame.Close()
def onChoice(self,event):
# get the style that choosed by the user
self.style = self.choice.GetString( self.choice.GetSelection())
def update_image_on_dnd(self, filepath):
# pass filepath -- https://www.blog.pythonlibrary.org/2017/10/25/wx_drag_and_drop_images/
self.on_view(filepath=filepath)
def on_view(self, filepath=None):
# set the image to the bitmap -- https://www.blog.pythonlibrary.org/2017/10/25/wx_drag_and_drop_images/
if not filepath:
filepath = self.photoTxt.GetValue()
img = wx.Image(filepath, wx.BITMAP_TYPE_ANY)
self.pathDir = filepath
# scale the image, preserving the aspect ratio
W = img.GetWidth()
H = img.GetHeight()
if W > H:
NewW = PhotoMaxSize
NewH = PhotoMaxSize * H / W
else:
NewH = PhotoMaxSize
NewW = PhotoMaxSize * W / H
img = img.Scale(NewW,NewH)
print(1)
self.imageCtrl.SetBitmap(wx.Bitmap(img))
self.panel.Refresh()
# output frame
class Output(wx.Frame):
def __init__(self):
# create output frame
wx.Frame.__init__(self)
self.frame = wx.Frame(None, title='Anima', size = (1600, 800))
self.panel = wx.Panel(self.frame)
self.panel.SetBackgroundColour(wx.Colour(233,243,255,255))
self.outputCreateWidgets()
self.frame.Show()
def outputCreateWidgets(self):
# create components for the output frame
title = "Anima"
lbl = wx.StaticText(self.panel,-1,style = wx.ALIGN_CENTER)
font = wx.Font(48, wx.FONTFAMILY_MODERN, wx.NORMAL, wx.NORMAL)
subFont = wx.Font(15, wx.FONTFAMILY_MODERN, wx.NORMAL, wx.NORMAL)
lbl.SetFont(font)
lbl.SetLabel(title)
# get message from pub
pub.subscribe(self.getMessage, "path")
# create the reset button
reset = wx.Button(self.panel, label = 'Reset')
reset.Bind(wx.EVT_BUTTON, self.OnReset)
# set the layout
self.mainSizer = wx.BoxSizer(wx.VERTICAL)
self.mainSizer.Add(lbl, 0, wx.CENTER, 5)
self.mainSizer.Add(wx.StaticLine(self.panel, wx.ID_ANY),
0, wx.ALL|wx.EXPAND, 5)
self.mainSizer.Add(reset, 0, wx.CENTER|wx.TOP, 20)
self.panel.SetSizer(self.mainSizer)
self.panel.Layout()
def OnReset(self, event):
# the pass the message and close current frame
pub.sendMessage("state", message = 'input')
self.frame.Close()
def getMessage(self, message, arg2=None):
# get messages from the pub
self.location = message
if arg2:
self.style = arg2
else:
self.style = shinkai
# scale the image, preserving the aspect ratio
imgbef = wx.Image(self.location, wx.BITMAP_TYPE_ANY)
W = imgbef.GetWidth()
H = imgbef.GetHeight()
if W > H:
NewW = maxSize
NewH = maxSize * H / W
else:
NewH = maxSize
NewW = maxSize * W / H
# open images that import by user and output by this application
imgbef = imgbef.Scale(NewW,NewH)
self.before = SB.GenStaticBitmap(self.panel, wx.ID_ANY, wx.Bitmap(imgbef), pos = ((1600 - imgbef.GetWidth() * 2)/2, 150))
self.before.SetBitmap(wx.Bitmap(imgbef))
filename = self.location.split(os.path.sep)[-1]
afterLocation = 'output/'+ self.style + '/adaptive/' + filename
imgAfter = wx.Image(afterLocation, wx.BITMAP_TYPE_ANY)
imgAfter = imgAfter.Scale(NewW, NewH)
self.after = SB.GenStaticBitmap(self.panel, wx.ID_ANY, wx.Bitmap(imgAfter), pos = ((1600 - imgbef.GetWidth() * 2)/2 + imgbef.GetWidth() + 20, 150))
self.after.SetBitmap(wx.Bitmap(imgAfter))
self.panel.Refresh()
class controller(wx.App):
# controller to control the frame
def __init__(self):
# create the application
wx.App.__init__(self)
self.frame = Input()
pub.subscribe(self.getMSG, "state")
def getMSG(self, message):
# set frame based on the message
if message == "input":
self.frame = Input()
else:
self.frame = Output()
# create this application and mainloop
if __name__ == "__main__":
app = controller()
app.MainLoop()