-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
34 lines (30 loc) · 1.02 KB
/
main.py
File metadata and controls
34 lines (30 loc) · 1.02 KB
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
from flask import Flask, request, render_template_string, send_file
from PIL import Image
import io
app = Flask(__name__)
HTML = '''
<!doctype html>
<title>PNG to ICO Converter 2.0</title>
<h1>Upload a PNG to convert to ICO222</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file accept="image/png">
<input type=submit value=Convert>
</form>
{% if ico_url %}
<a href="{{ ico_url }}">Download ICO</a>
{% endif %}
'''
@app.route('/', methods=['GET', 'POST'])
def index():
ico_url = None
if request.method == 'POST':
file = request.files.get('file')
if file and file.filename.lower().endswith('.png'):
img = Image.open(file.stream)
ico_bytes = io.BytesIO()
img.save(ico_bytes, format='ICO')
ico_bytes.seek(0)
return send_file(ico_bytes, mimetype='image/x-icon', as_attachment=True, download_name='icon.ico')
return render_template_string(HTML, ico_url=ico_url)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=3366)