This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
46 lines (39 loc) · 1.62 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
from flask import Flask
from flask import render_template
from flask import request, redirect, url_for
from flask import send_from_directory
from werkzeug.utils import secure_filename
from datetime import datetime
from PIL import Image, ImageSequence
import os
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = '/tmp/uploads/'
app.config['DOWNLOAD_FOLDER'] = '/tmp/downloads/'
if os.path.exists(app.config['UPLOAD_FOLDER']) == False:
os.mkdir(app.config['UPLOAD_FOLDER'])
if os.path.exists(app.config['DOWNLOAD_FOLDER']) == False:
os.mkdir(app.config['DOWNLOAD_FOLDER'])
@app.route('/', methods=['GET', 'POST'])
def index():
return render_template("index.html")
@app.route('/downloads/<filename>')
def download_file(filename):
return send_from_directory(app.config['DOWNLOAD_FOLDER'],
filename)
@app.route('/upload/', methods=['POST'])
def upload_file():
if request.method == 'POST':
f = request.files['gif']
f_name = datetime.now().strftime("%Y%m%d%H%M%S-") + secure_filename(f.filename)
if f_name[-4:] != '.gif':
return "Error: File type not support."
f.save(app.config['UPLOAD_FOLDER']+f_name)
with Image.open(app.config['UPLOAD_FOLDER']+f_name) as im:
if im.is_animated:
frames = [f.copy() for f in ImageSequence.Iterator(im)]
frames.reverse()
frames[0].save(os.path.join(app.config['DOWNLOAD_FOLDER'],f_name), save_all=True, append_images=frames[1:])
return f_name
return "Error: Not Allowed."
if __name__ == '__main__':
app.run()