Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,58 @@
|
|
| 1 |
-
from flask import Flask, request,
|
| 2 |
-
import
|
|
|
|
|
|
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
@app.route('/convert', methods=['POST'])
|
| 7 |
def convert():
|
| 8 |
-
files = request.files.getlist('
|
| 9 |
-
height = int(request.form
|
| 10 |
-
width = int(request.form
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
if __name__ == '__main__':
|
| 17 |
app.run(host='0.0.0.0', port=5000)
|
|
|
|
| 1 |
+
from flask import Flask, request, send_from_directory
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import os
|
| 4 |
+
import tempfile
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
+
def convert_images(image_paths, height, width):
|
| 9 |
+
converted_files = []
|
| 10 |
+
for image_path in image_paths:
|
| 11 |
+
img = Image.open(image_path).convert("RGBA")
|
| 12 |
+
orig_width, orig_height = img.size
|
| 13 |
+
|
| 14 |
+
if orig_height > height or orig_width > width:
|
| 15 |
+
if orig_height / height > orig_width / width:
|
| 16 |
+
new_height = height
|
| 17 |
+
new_width = int((new_height / orig_height) * orig_width)
|
| 18 |
+
else:
|
| 19 |
+
new_width = width
|
| 20 |
+
new_height = int((new_width / orig_width) * orig_height)
|
| 21 |
+
else:
|
| 22 |
+
new_width, new_height = orig_width, orig_height
|
| 23 |
+
|
| 24 |
+
img = img.resize((new_width, new_height), Image.LANCZOS)
|
| 25 |
+
|
| 26 |
+
base_name = os.path.splitext(os.path.basename(image_path))[0]
|
| 27 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".webp", prefix=base_name + "_")
|
| 28 |
+
img.save(temp_file, format='WEBP')
|
| 29 |
+
temp_file.close()
|
| 30 |
+
|
| 31 |
+
converted_files.append(temp_file.name)
|
| 32 |
+
|
| 33 |
+
return converted_files
|
| 34 |
+
|
| 35 |
@app.route('/convert', methods=['POST'])
|
| 36 |
def convert():
|
| 37 |
+
files = request.files.getlist('images')
|
| 38 |
+
height = int(request.form.get('height', 100))
|
| 39 |
+
width = int(request.form.get('width', 200))
|
| 40 |
+
|
| 41 |
+
image_paths = []
|
| 42 |
+
for file in files:
|
| 43 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[1])
|
| 44 |
+
file.save(temp_file.name)
|
| 45 |
+
image_paths.append(temp_file.name)
|
| 46 |
+
|
| 47 |
+
converted_files = convert_images(image_paths, height, width)
|
| 48 |
+
|
| 49 |
+
return {
|
| 50 |
+
"converted_files": [os.path.basename(f) for f in converted_files]
|
| 51 |
+
}
|
| 52 |
|
| 53 |
+
@app.route('/download/<filename>', methods=['GET'])
|
| 54 |
+
def download(filename):
|
| 55 |
+
return send_from_directory(tempfile.gettempdir(), filename)
|
| 56 |
|
| 57 |
if __name__ == '__main__':
|
| 58 |
app.run(host='0.0.0.0', port=5000)
|