-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
419 lines (382 loc) · 16.9 KB
/
app.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# ================
# GUI class
# ================
# -----------------
# threading library
import threading
# ---------------------
# GUI-related libraries
from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog
# -----------------
# better fonts
import ttk, tkFont
# -----------------------
# python image library
from PIL import Image, ImageTk, ImageDraw, ExifTags
import PIL.Image
# -----------------------
# basic pythong libraries
from math import *
import os, sys, time, unicodedata, operator
# -----------------------
# probably need numeric pythong
import numpy as np
# -----------------------
from image_bar import *
from image_class import *
from video_class import *
# ================
# GUI class
# inherent from tk
# ================
# -----------------
class root_gui_class(Tk):
def __init__(self, event_caller):
## ------------------------------------
## start tkinter window and title
time.sleep(1)
Tk.__init__(self)
self.event_caller = event_caller
self.title("Controlled Lapse")
self.minsize(300, 300)
self.geometry("800x600")
os.chdir(os.getcwd())
self.width = 800
self.height = 600
self.files = []
self.view_image = None
self.videos = []
self.video_labels = []
self.active_video = -1
self.zoom = 5.0
self.project = ''
self.file_save = ''
self.save_directory = os.getcwd()
## ------------------------------------
## ------------------------------------
## Menu Bar
self.files = []
self.menu_bar = Menu(self)
self.config(menu=self.menu_bar)
# -- File Menu
self.file_menu = Menu(self.menu_bar, tearoff=0)
self.file_menu.add_command(label="New Project", command=lambda: self.new_project())
self.file_menu.add_command(label="Load Project", command=lambda: self.load_project())
self.file_menu.add_command(label="Save Project", command=lambda: self.save_project())
self.file_menu.add_command(label="Save Project As", command=lambda: self.save_project_as())
self.file_menu.add_command(label="Load Image Folder", command=lambda: self.add_directory())
self.file_menu.add_command(label="Exit", command=self.quit)
self.menu_bar.add_cascade(label="File", menu=self.file_menu)
## ------------------------------------
## ------------------------------------
## Video Options
self.edit_menu = Menu(self.menu_bar, tearoff=0)
self.edit_menu.add_command(label="Split at Current Frame", command=lambda: self.splice())
self.edit_menu.add_command(label="Make Current Video", command=lambda: self.make())
self.edit_menu.add_command(label="Make All Videos", command=lambda: self.make_all())
self.menu_bar.add_cascade(label="Edit", menu=self.edit_menu)
self.menu_bar.add_command(label="+", command=lambda: self.zoom_in())
self.menu_bar.add_command(label="-", command=lambda: self.zoom_out())
## ------------------------------------
## ------------------------------------
## create paned window that should
## cover everything in view
self.window = PanedWindow(self, orient=VERTICAL, sashwidth=5, bg="#888888", opaqueresize=FALSE)
self.window.pack(fill=BOTH, expand=1)
## ------------------------------------
## ------------------------------------
## add top area with image viewer and
## clip explorer
self.main = Frame(self.window, bg='black')
self.main.pack(fill=BOTH, expand=YES)
self.main.grid_rowconfigure(0, weight=1)
self.main.grid_columnconfigure(0, weight=1)
## ------------------------------------
## ------------------------------------
## active image viewer
self.window.add(self.main, stretch='always')
self.viewer = Frame(self.main, bg='black', padx=5, pady=5)
Canvas(self.viewer)
self.viewer.grid(row=0, column=0, sticky=NSEW)
## ------------------------------------
## ------------------------------------
## clip explorer
self.clips = Frame(self.main, bg='#555555', width=200, padx=5, pady=5)
Canvas(self.clips)
self.clips.grid(row=0, column=1, sticky=NSEW)
## ------------------------------------
## ------------------------------------
## add timeline viewer
self.timeline = Frame(self.window, bg='black')
self.image_bar = Frame(self.timeline, bg='black')
self.image_bar.pack(fill=BOTH)
self.window.add(self.timeline, height=75, minsize=30, stretch="never")
## ------------------------------------=
## ------------------------------------
## add binds for user interactions
self.resize = '' ### event handler to delay resizing so GUI doesn't get choked up
self.window.bind('<B1-Motion>', self.window_click)
self.bind('<Configure>', self.schedule_resize)
## ------------------------------------
def new_project(self):
## ------------------------------------
## load a new directory
self.project = tkFileDialog.asksaveasfilename()
self.file_save = self.project
self.save_directory = os.path.dirname(self.file_save)
print 'file', self.project
## ------------------------------------
def load_project(self):
## ------------------------------------
## load a new directory
self.project = tkFileDialog.askopenfilename()
self.file_save = self.project
self.save_directory = os.path.dirname(self.file_save)
print 'opening file', self.project
## ------------------------------------
f = open(self.project, 'r')
n_videos = int(f.readline().split()[-1])
print 'reading in', n_videos, 'clips'
## ------------------------------------
self.files = []
self.videos = []
self.active_video = 0
for i in range(0, n_videos):
line = f.readline().split()
name = ' '.join(line[1:-4])
rate = int(line[-3])
frames = int(line[-1])
files = []
for j in range(0, frames):
line = f.readline().replace('\n','').replace('\r','').replace('\\\\','\\').split()[-1]
directory = os.path.dirname(line)
filename = os.path.basename(line)
file = image_class(filename, directory, id=j)
files.append(file)
self.files.append(files)
active = False
if i == 0:
active = True
print 'current directory', os.getcwd()
print 'changing directory', files[-1].directory
os.chdir(files[-1].directory)
self.videos.append(video_class(i, name, self.files[-1], self.clips, self, active, rate=rate))
## ------------------------------------
def save_project_as(self):
## ------------------------------------
## load a new directory
self.file_save = tkFileDialog.asksaveasfilename()
self.save_directory = os.path.dirname(self.file_save)
self.project = self.file_save
print 'file', self.project
self.save_project()
## ------------------------------------
def add_directory(self):
## ------------------------------------
## load a new directory
self.directory = tkFileDialog.askdirectory()
os.chdir(self.directory)
## ------------------------------------
## ------------------------------------
## get a list of images
files = []
for file in os.listdir(self.directory):
if '.jpg' in file or '.png' in file:
## ----------------------------
## check for corrupt files
try:
## Image.open(file)
if file.split('.')[0].isdigit():
files.append(image_class(file, self.directory, id=int(file.split('.')[0])))
else:
files.append(image_class(file, self.directory))
except:
print 'could not open', file
## ----------------------------
if len(files) > 1:
## sort numerically and rename in order
files.sort(key=operator.attrgetter('id'))
for i in range(0, len(files)):
files[i].id = i
self.files.append(files)
## ------------------------------------
## ------------------------------------
## destroy and create new image_bar_class
if (len(self.files) > 1):
for widget in self.timeline.winfo_children():
widget.destroy()
self.image_bar = image_bar_class(self.files[self.active_video], self.timeline, self, zoom=self.zoom)
self.image_bar.pack(fill=BOTH)
## self.bind('<B1-Motion>', self.image_bar.click)
self.image_bar.bind('<MouseWheel>', self.image_bar.wheel)
## ------------------------------------
## ------------------------------------
## update main viewer
self.show_image(self.files[-1][len(self.files[-1])/2])
print 'active video plus 1'
self.active_video += 1
self.videos.append(video_class(self.active_video, 'video ' + repr(self.active_video+1), self.files[self.active_video], self.clips, self, True))
for widget in self.clips.winfo_children():
widget.destroy()
for i in range(0, len(self.videos)):
if i == self.active_video:
self.videos[i].active = True
else:
self.videos[i].active = False
self.videos[i].draw()
self.clips.update()
self.timeline.update()
## ------------------------------------
def show_image(self, image):
for widget in self.viewer.winfo_children():
widget.destroy()
self.view_image_id = image.id
self.view_image = ImageTk.PhotoImage(PIL.Image.open(image.filename))
self.view_image_label = Label(self.viewer, image=self.view_image,
borderwidth=0,
compound="center",
highlightthickness = 0,
padx=0,
pady=0,
bg='black',
anchor=CENTER)
self.view_image_label.pack(fill=BOTH, expand=TRUE)
def make(self):
if self.active_video > -1:
self.videos[self.active_video].make_video()
def make_all(self):
if self.active_video > -1:
for i in range(0, len(self.videos)):
self.videos[i].make_video()
def splice(self):
if self.active_video > -1:
files = self.videos[self.active_video].files[self.view_image_id:]
self.videos[self.active_video].files = self.videos[self.active_video].files[:self.view_image_id]
self.videos.insert(self.active_video+1, video_class(self.active_video+1, 'video ' + repr(self.active_video+2), files, self.clips, self, True))
self.active_video += 1
for i in range(0, len(files)):
files[i].id = i
self.files = self.files[:self.view_image_id]
self.files.append(files)
for widget in self.clips.winfo_children():
widget.destroy()
for i in range(0, len(self.videos)):
if i == self.active_video:
self.videos[i].active = True
else:
self.videos[i].active = False
self.videos[i].draw()
self.clips.update()
self.view_image_id = len(self.files[self.active_video])/2
## ------------------------------------
## destroy and create new image_bar_class
if (len(self.files) > 0):
print 'creating new image_bar'
for widget in self.timeline.winfo_children():
widget.destroy()
self.image_bar = image_bar_class(self.files[self.active_video], self.timeline, self, zoom=self.zoom)
self.image_bar.pack(fill=BOTH)
## self.bind('<B1-Motion>', self.image_bar.click)
## ------------------------------------
def zoom_in(self):
self.zoom = self.zoom * 1.1
## ------------------------------------
## destroy and create new image_bar_class
if (len(self.files) > 0):
print 'creating new image_bar'
for widget in self.timeline.winfo_children():
widget.destroy()
self.image_bar = image_bar_class(self.files[self.active_video], self.timeline, self, zoom=self.zoom)
self.image_bar.pack(fill=BOTH)
## self.bind('<B1-Motion>', self.image_bar.click)
## ------------------------------------
def zoom_out(self):
self.zoom = self.zoom / 1.1
## ------------------------------------
## destroy and create new image_bar_class
if (len(self.files) > 0):
print 'creating new image_bar'
for widget in self.timeline.winfo_children():
widget.destroy()
self.image_bar = image_bar_class(self.files[self.active_video], self.timeline, self, zoom=self.zoom)
self.image_bar.pack(fill=BOTH)
## self.bind('<B1-Motion>', self.image_bar.click)
## ------------------------------------
def select_video(self, id):
if self.active_video != id:
self.active_video = id
for widget in self.clips.winfo_children():
widget.destroy()
for i in range(0, len(self.videos)):
if i == self.active_video:
self.videos[i].active = True
else:
self.videos[i].active = False
self.videos[i].draw()
self.clips.update()
self.view_image_id = len(self.files[self.active_video])/2
## ------------------------------------
## destroy and create new image_bar_class
if (len(self.files) > 0):
print 'creating new image_bar'
for widget in self.timeline.winfo_children():
widget.destroy()
self.image_bar = image_bar_class(self.files[self.active_video], self.timeline, self, zoom=self.zoom)
self.image_bar.pack(fill=BOTH)
## self.bind('<B1-Motion>', self.image_bar.click)
## ------------------------------------
def window_click(self, event):
if event.widget is self.window:
if self.resize == '':
pass
else:
self.after_cancel(self.resize)
self.resize = self.after(200, self.on_resize)
self.update_idletasks()
def schedule_resize(self, event):
if event.widget is self:
if self.resize == '':
pass
else:
self.after_cancel(self.resize)
self.resize = self.after(200, self.on_resize)
def save_project(self):
f = open(self.file_save, 'w')
f.write('clips: ' + repr(len(self.videos)) + '\n')
for video in self.videos:
f.write('name: ' + video.name + ' rate: ' + repr(video.rate) + ' frames: '+repr(len(video.files)) + '\n')
for file in video.files:
f.write('image: ' + file.directory.replace('/','\\') + '\\' + file.filename + '\n')
f.close()
def on_resize(self):
if (self.resize != ''):
## ------------------------------------
## destroy and create new image_bar_class
if (len(self.files) > 0):
print 'creating new image_bar'
for widget in self.timeline.winfo_children():
widget.destroy()
self.image_bar = image_bar_class(self.files[self.active_video], self.timeline, self, zoom=self.zoom)
self.image_bar.pack(fill=BOTH)
## self.bind('<B1-Motion>', self.image_bar.click)
## ------------------------------------
self.resize = ''
## ====================================
# ==========================
# main initializing function
# ==========================
def main():
# create event caller for threads
event_caller = threading.Event()
# initialize GUI
root = root_gui_class(event_caller)
## root.add_directory()
# start GUI mainloop
root.mainloop()
# ================
# start everything
# ----------------
main()
# ----------------
# ================