Skip to content

Instantly share code, notes, and snippets.

@hiepph
Forked from jdhao/resize_and_pad_image_to_square
Created November 13, 2019 07:32
Show Gist options
  • Save hiepph/ec427dcb0859b94368f3e5ccebd830a7 to your computer and use it in GitHub Desktop.
Save hiepph/ec427dcb0859b94368f3e5ccebd830a7 to your computer and use it in GitHub Desktop.
this script will resize and pad an image to desired square size and keep its aspect ratio unchanged. Before running the script, please change the size and image path to valid value.
from PIL import Image, ImageOps
import cv2
desired_size = 368
im_pth = "/home/jdhao/test.jpg"
# im = Image.open(im_pth)
# old_size = im.size # old_size[0] is in (width, height) format
# ratio = float(desired_size)/max(old_size)
# new_size = tuple([int(x*ratio) for x in old_size])
## using thumbnai() or resize() method to resize the input image and keep its aspect ratio
# im.thumbnail(new_size, Image.ANTIALIAS)
# im = im.resize(new_size, Image.ANTIALIAS)
## create a new square image with desired size and paste the resized image onto it.
# new_im = Image.new("RGB", (desired_size, desired_size))
# new_im.paste(im, ((desired_size-new_size[0])//2,
# (desired_size-new_size[1])//2))
## or we can expand the resized image by adding borders to its 4 side
# delta_w = desired_size - new_size[0]
# delta_h = desired_size - new_size[1]
# padding = (delta_w//2, delta_h//2, delta_w-(delta_w//2), delta_h-(delta_h//2))
# print(padding)
# new_im = ImageOps.expand(im, padding, fill="black")
# new_im.show()
## opencv has copyMakeBorder() method which is handy for making borders
im = cv2.imread(im_pth)
old_size = im.shape[:2] # old_size is in (height, width) format
ratio = float(desired_size)/max(old_size)
new_size = tuple([int(x*ratio) for x in old_size])
# new_size should be in (width, height) format
im = cv2.resize(im, (new_size[1], new_size[0]))
delta_w = desired_size - new_size[1]
delta_h = desired_size - new_size[0]
top, bottom = delta_h//2, delta_h-(delta_h//2)
left, right = delta_w//2, delta_w-(delta_w//2)
color = [0, 0, 0]
new_im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT,
value=color)
cv2.imshow("image", new_im)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment