Station icons: Keep aspect ratio when scaling

This commit is contained in:
milaq 2019-08-25 19:23:37 +02:00
parent 72a8df3ed9
commit 4b45aa58d0

View file

@ -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()