-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
51 lines (38 loc) · 1.43 KB
/
Copy pathapp.py
File metadata and controls
51 lines (38 loc) · 1.43 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from flask import Flask, render_template, request, send_from_directory, jsonify
from PIL import Image
import os
import uuid
app = Flask(__name__)
# Ensure output folders exist
os.makedirs('static/converted', exist_ok=True)
os.makedirs('static/stickers', exist_ok=True)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/convert', methods=['POST'])
def convert():
image = request.files['image']
output_format = request.form['format'] # png, jpg, gif, webp
if image:
img = Image.open(image)
filename = f"{uuid.uuid4()}.{output_format}"
save_path = os.path.join('static/converted', filename)
# Convert to RGB if saving to JPEG (no transparency)
if output_format == 'jpg':
img = img.convert('RGB')
img.save(save_path)
return jsonify({"status": "success", "url": '/' + save_path})
return jsonify({"status": "error"})
@app.route('/sticker', methods=['POST'])
def sticker():
image = request.files['image']
if image:
img = Image.open(image).convert('RGBA') # ensure transparency
img = img.resize((512, 512))
filename = f"{uuid.uuid4()}.png"
save_path = os.path.join('static/stickers', filename)
img.save(save_path, format='PNG')
return jsonify({"status": "success", "url": '/' + save_path})
return jsonify({"status": "error"})
if __name__ == '__main__':
app.run(debug=True)