feat: add image resizing functionality to limit height to 50px during game addition

This commit is contained in:
Manuel Weiser 2024-09-03 14:54:14 +02:00
parent 4072b9e606
commit 47dade0384

View File

@ -8,6 +8,7 @@ from datetime import datetime
import requests
from bs4 import BeautifulSoup
import base64
from PIL import Image
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(app.instance_path, 'games.db')
@ -25,6 +26,14 @@ def encode_image_to_base64(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
def resize_image(image_path, max_height=50):
with Image.open(image_path) as img:
aspect_ratio = img.width / img.height
new_height = min(max_height, img.height)
new_width = int(aspect_ratio * new_height)
img = img.resize((new_width, new_height), Image.ANTIALIAS)
img.save(image_path)
@app.route('/games', methods=['POST'])
def add_game():
if not authenticate():
@ -40,6 +49,9 @@ def add_game():
local_image_path = os.path.join(app.instance_path, image_filename)
with open(local_image_path, 'wb') as f:
f.write(image_response.content)
# Resize the image
resize_image(local_image_path)
else:
return jsonify({'message': 'Image could not be downloaded!'}), 400