From 1707b4029246a67617a367b6d76fd31c14b96e90 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 4 Feb 2020 19:26:12 -0800 Subject: [PATCH] Add imagetools.py. --- voussoirkit/imagetools.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 voussoirkit/imagetools.py diff --git a/voussoirkit/imagetools.py b/voussoirkit/imagetools.py new file mode 100644 index 0000000..9f979c2 --- /dev/null +++ b/voussoirkit/imagetools.py @@ -0,0 +1,19 @@ +def fit_into_bounds(image_width, image_height, frame_width, frame_height, only_shrink=False): + ''' + Given the w+h of the image and the w+h of the frame, + return new w+h that fits the image into the frame + while maintaining the aspect ratio. + + (1920, 1080, 400, 400) -> (400, 225) + ''' + width_ratio = frame_width / image_width + height_ratio = frame_height / image_height + ratio = min(width_ratio, height_ratio) + + new_width = int(image_width * ratio) + new_height = int(image_height * ratio) + + if only_shrink and (new_width > image_width or new_height > image_height): + return (image_width, image_height) + + return (new_width, new_height)