Last active
December 3, 2025 07:02
-
-
Save IdeaKing/11cf5e146d23c5bb219ba3508cca89ec to your computer and use it in GitHub Desktop.
Resize image with padding using CV2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| from typing import Tuple | |
| def resize_with_pad(image: np.array, | |
| new_shape: Tuple[int, int], | |
| padding_color: Tuple[int] = (255, 255, 255)) -> np.array: | |
| """Maintains aspect ratio and resizes with padding. | |
| Params: | |
| image: Image to be resized. | |
| new_shape: Expected (width, height) of new image. | |
| padding_color: Tuple in BGR of padding color | |
| Returns: | |
| image: Resized image with padding | |
| """ | |
| original_shape = (image.shape[1], image.shape[0]) | |
| ratio = float(max(new_shape))/max(original_shape) | |
| new_size = tuple([int(x*ratio) for x in original_shape]) | |
| image = cv2.resize(image, new_size) | |
| delta_w = new_shape[0] - new_size[0] | |
| delta_h = new_shape[1] - new_size[1] | |
| top, bottom = delta_h//2, delta_h-(delta_h//2) | |
| left, right = delta_w//2, delta_w-(delta_w//2) | |
| image = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=padding_color) | |
| return image | |
| if __name__ == "__main__": | |
| image = cv2.imread("/path/to/image") | |
| image = resize_with_pad(image, (256, 256)) | |
| cv2.imshow("Padded image", image) | |
| cv2.waitKey() |
Author
i like to suggest an optimization to work with any image size, the original code have a problem when the min dimension leng of the image after resize its bigger than the objective size in that dimension, only add this lines:
` Suggestion
if new_size[0] > new_shape[0] or new_size[1] > new_shape[1]: ratio = float(min(new_shape)) / min(original_shape) new_size = tuple([int(x * ratio) for x in original_shape])` that adjusts the ratio to resize the image in the range of objective size
the final function is this:
` Final Fnction
def resize_with_pad(image: np.array, new_shape: Tuple[int, int], padding_color: Tuple[int] = (255, 255, 255)) -> np.array: original_shape = (image.shape[1], image.shape[0]) ratio = float(max(new_shape))/max(original_shape) new_size = tuple([int(x*ratio) for x in original_shape]) if new_size[0] > new_shape[0] or new_size[1] > new_shape[1]: ratio = float(min(new_shape)) / min(original_shape) new_size = tuple([int(x * ratio) for x in original_shape]) image = cv2.resize(image, new_size) delta_w = new_shape[0] - new_size[0] delta_h = new_shape[1] - new_size[1] top, bottom = delta_h//2, delta_h-(delta_h//2) left, right = delta_w//2, delta_w-(delta_w//2) image = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT,None,value=padding_color) return image`
Thanks! I will test it out and add it to the original.
Author
In case of downsizing, delta_w or delta_h will become negative. The following is a fix:
delta_w = new_shape[0] - new_size[0] if new_shape[0] > new_size[0] else 0 delta_h = new_shape[1] - new_size[1] if new_shape[1] > new_size[1] else 0
Will take a look...
Thank you @IdeaKing !
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In case of downsizing, delta_w or delta_h will become negative. The following is a fix: