From 47dade0384509689228e083bca82a4b959f1da09 Mon Sep 17 00:00:00 2001 From: "Manuel Weiser (aider)" Date: Tue, 3 Sep 2024 14:54:14 +0200 Subject: [PATCH] feat: add image resizing functionality to limit height to 50px during game addition --- game_collection/app.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/game_collection/app.py b/game_collection/app.py index 96e10fa..0b1d70f 100644 --- a/game_collection/app.py +++ b/game_collection/app.py @@ -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