From 4b45aa58d0ddfa2ae1c7022a9c5dd1eb17d75a04 Mon Sep 17 00:00:00 2001 From: milaq Date: Sun, 25 Aug 2019 19:23:37 +0200 Subject: [PATCH] Station icons: Keep aspect ratio when scaling --- ycast/station_icons.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ycast/station_icons.py b/ycast/station_icons.py index 6fa8b52..39c5aba 100644 --- a/ycast/station_icons.py +++ b/ycast/station_icons.py @@ -24,7 +24,11 @@ def get_icon_from_url(iconurl): try: image = Image.open(io.BytesIO(response.content)) image = image.convert("RGB") - image = image.resize((MAX_SIZE, MAX_SIZE), Image.ANTIALIAS) # TODO: keep aspect ratio + if image.size[0] > image.size[1]: + ratio = MAX_SIZE / image.size[0] + else: + ratio = MAX_SIZE / image.size[1] + image = image.resize((int(image.size[0] * ratio), int(image.size[1] * ratio)), Image.ANTIALIAS) with io.BytesIO() as output_img: image.save(output_img, format="JPEG") image_conv = output_img.getvalue()