-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
40 lines (34 loc) · 1.31 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
from flask import Flask, request, redirect, url_for, send_file, render_template
from werkzeug.utils import secure_filename
from PIL import Image
import os
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['ALLOWED_EXTENSIONS'] = {'webp'}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
@app.route('/')
def upload_form():
return render_template('upload.html')
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
png_filepath = convert_webp_to_png(filepath)
return send_file(png_filepath, as_attachment=True)
return redirect(request.url)
def convert_webp_to_png(filepath):
png_filepath = filepath.rsplit('.', 1)[0] + '.png'
with Image.open(filepath) as img:
img.save(png_filepath, 'PNG')
return png_filepath
if __name__ == '__main__':
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
app.run(debug=True)