-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgifsplit.py
66 lines (53 loc) · 2.03 KB
/
gifsplit.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
from flask import *
from PIL import ImageSequence, Image
import json
import hashlib
import os
import threading
import time
import cleanup
class Clean(threading.Thread):
def __init__(self, folder, filter_seconds, sleep_time):
super().__init__()
self.lock = threading.Lock()
self.is_using = False
self.secs = filter_seconds
self.folder = folder
self.sleep_time = sleep_time
self.daemon = True #daemonize cleanup thread
def run(self):
while True:
if not self.is_using:
with self.lock:
cleanup.clear_cache(self.folder, self.secs)
time.sleep(self.sleep_time)
cleanup_thread = Clean("uploads/", 60 * 5, 60 * 10)
cleanup_thread.start()
app = Flask(__name__)
@app.route("/")
def index(name=None):
return render_template("index.html", name=name)
@app.route("/uploads/<filename>")
def uploads(filename):
return send_file(f"uploads/{filename}")
@app.route("/api/split", methods=['POST'])
def split():
cleanup_thread.is_using = True
frames = []
try:
if request.method == 'POST':
file = request.files['image']
# Getting the hash of the file contents. This can help avoid duplication
file_hash = hashlib.md5(file.stream.read()).hexdigest()
with Image.open(file) as im:
for i, frame in enumerate(ImageSequence.Iterator(im)):
#creating a hash for all the frames. The hashing helps with duplication issues.
frame_hash = hashlib.md5(f"{file_hash}{i}".encode()).hexdigest()
if not os.path.exists(f"uploads/{frame_hash}.png"):
frame.save(f"uploads/{frame_hash}.png", format="PNG")
frames.append(f"uploads/{frame_hash}.png")
cleanup_thread.is_using = False
return json.dumps({"frames": frames})
except:
cleanup_thread.is_using = False
return make_response(json.dumps({}), 400)