From 830f4e75e5c95035fb2ff9df6eac01c565ca8556 Mon Sep 17 00:00:00 2001 From: ThanhNS Date: Wed, 17 Oct 2018 14:37:15 +0700 Subject: [PATCH] add resize image w/u aspect ratio function --- yolo/utils.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 yolo/utils.py diff --git a/yolo/utils.py b/yolo/utils.py new file mode 100644 index 0000000..0915d17 --- /dev/null +++ b/yolo/utils.py @@ -0,0 +1,16 @@ +from PIL import Image + + +def letterbox_image(image, size): + '''Resize image with unchanged aspect ratio using padding''' + + img_width, img_height = image.size + w, h = size + scale = min(w / img_width, h / img_height) + nw = int(img_width * scale) + nh = int(img_height * scale) + + image = image.resize((nw, nh), Image.BICUBIC) + new_image = Image.new('RGB', size, (128, 128, 128)) + new_image.paste(image, ((w - nw) // 2, (h - nh) // 2)) + return new_image