This repository has been archived by the owner on Apr 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
205 lines (169 loc) · 5.97 KB
/
run.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
import datetime
from flask import Flask, render_template, send_from_directory, jsonify, request
import os
import sys
import time
import threading
import webbrowser
from shutil import copyfile
from PIL import Image
if sys.version_info.major >= 3:
from tkinter import Tk
from tkinter.filedialog import askdirectory, askopenfilenames
else:
from Tkinter import Tk
try:
from Tkinter.filedialog import askdirectory, askopenfilenames
except:
from tkFileDialog import askdirectory as askdirectory
from tkFileDialog import askopenfilenames as askopenfilenames
supported_extensions = ['.jpg', '.jpeg', '.png', '.svg', '.bmp', '.ico', '.gif']
def select_directory():
""" Opens a select directory dialog and returns the path selected """
root = Tk()
root.withdraw()
root.wm_attributes('-topmost', 1)
directory = askdirectory(parent=root)
return directory
def select_files():
""" Opens a select files dialog and returns the files selected """
root = Tk()
root.withdraw()
root.wm_attributes('-topmost', 1)
files = askopenfilenames(parent=root,
title="Select file",
filetypes=(("Image files", '*' + ';*'.join(supported_extensions)), ("all files", "*.*"))
)
return root.tk.splitlist(files)
def get_files_from_directory(directory):
""" Gets files with supported file extensions within a directory """
file_list = []
for root, dirs, files in os.walk(directory.replace('/', '\\'), topdown=False):
for name in files:
if os.path.splitext(name)[1] in supported_extensions:
file_list.append(os.path.join(root, name))
return file_list
def add_images_to_dict(image_dict, image_list):
""" Adds a new image to a dictionary object. image_dict[next_index] = {'location' : location...}"""
if len(image_dict) > 0:
next_value = max([int(i) for i in image_dict]) + 1
else:
next_value = 1
files = [image_dict[i]['location'] for i in image_dict]
for image in image_list:
if image not in files:
image_dict[str(next_value)] = {
'location' : image,
'size' : round(os.path.getsize(image) / 1000000, 2),
'date' : get_exif(image, 'date'),
'model' : get_exif(image, 'model')
}
next_value += 1
def get_exif(file, data):
try: # Also handles if exif is not None
exif = Image.open(file)._getexif()
if data == 'date' and 36867 in exif:
return exif[36867]
if data == 'model' and 272 in exif:
return exif[272]
except:
pass
return 'N/A'
def copy_media(items, dest):
""" Copies files from their location to a new destination """
for file in items:
filename = os.path.basename(file)
copyfile(file, dest + '\\' + filename)
def move_media(items, dest):
""" Moves files from their location to a new destination """
for file in items:
filename = os.path.basename(file)
os.rename(file, dest + '\\' + filename)
images = {}
# Get Args
input_folder = ''
if len(sys.argv) > 1:
image_files = get_files_from_directory(sys.argv[1])
add_images_to_dict(images, image_files)
class PingThread(threading.Thread):
timeout = 0
EXTEND_TIME = 60
def __init__(self):
super(PingThread, self).__init__()
self.refresh()
def run(self):
while True:
if datetime.datetime.now() > self.timeout:
print ("No client found, shutting server down")
os._exit(0)
time.sleep(5)
def refresh(self):
self.timeout = datetime.datetime.now() + datetime.timedelta(0, self.EXTEND_TIME)
app = Flask(__name__, static_url_path='')
@app.route('/')
def rootRoute():
return render_template('main.html')
@app.route('/getImages')
def getImagesRoute():
return jsonify(images)
@app.route('/image<id>')
def getImageRoute(id):
directory, filename = os.path.split(images[id]['location'])
return send_from_directory(directory, filename)
@app.route('/selectDirectory')
def selectDirectoryRoute():
directory = select_directory()
files = get_files_from_directory(directory)
add_images_to_dict(images, files)
return jsonify(images)
@app.route('/selectFiles')
def selectFilesRoute():
files = select_files()
add_images_to_dict(images, files)
return jsonify(images)
@app.route('/clearImages')
def clearImagesRoute():
images.clear()
return jsonify(images)
@app.route('/exportCopy', methods=['POST'])
def exportCopyRoute():
items = request.json
try:
output_dir = select_directory()
file_locations = [images[i]['location'] for i in items]
copy_media(file_locations, output_dir)
except Exception as e:
return jsonify({'success': False, 'message' : str(e)})
return jsonify({'success': True})
@app.route('/exportMove', methods=['POST'])
def exportMoveRoute():
items = request.json
try:
output_dir = select_directory()
file_locations = [images[i]['location'] for i in items]
move_media(file_locations, output_dir)
except Exception as e:
return jsonify({'success': False, 'message': str(e)})
return jsonify({'success': True})
@app.route('/ping')
def pingRoute():
pingThread.refresh()
return jsonify({'success': True})
@app.after_request
def add_header(r):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
pingThread = PingThread()
pingThread.start()
ip = "127.0.0.1"
port = 8080
webbrowser.open('http://' + ip + ':' + str(port) + '/', new=2, autoraise=True)
print("Site starting on http://" + ip + ":" + str(port))
app.run(host=ip, port=port)