Improve error handling in aspect_ratio()

This commit is contained in:
KV 2020-08-29 20:47:11 +02:00
parent ede29cbc95
commit 7c2fdd6d5a

View File

@ -165,11 +165,10 @@ def aspect_ratio(image_src):
image = Image.open(image_src)
if image.width > 0 and image.height > 0:
return image.width / image.height
print(f'aspect_ratio(): image size is {image.width} x {image.height}')
except ModuleNotFoundError as error:
print(f'aspect_ratio(): {error}')
except FileNotFoundError as error:
print(f'aspect_ratio(): {error}')
print(f'aspect_ratio(): Invalid image size {image.width} x {image.height}')
# ModuleNotFoundError and FileNotFoundError are the most expected, but all are handled equally.
except Exception as error:
print(f'aspect_ratio(): {type(error).__name__}: {error}')
return 1 # Assume 1:1 when unable to read actual image size