18 from typing
import Tuple
25 def resizeWithPadding(image: np.ndarray, width: int, height: int) -> Tuple[np.ndarray, int, int]:
27 Resizes the image while maintaining original aspect ratio,
28 and filling the remaining space with symmetrical padding so
29 that the original image is in the center
34 Input image as an array
36 Width of the output image
38 Height of the output image
42 Tuple[np.ndarray, int, int] -> Output image as numpy array,
43 number of pixels of padding from top/bottom and number of pixels
44 of padding from left/right
47 originalWidth = image.shape[1]
48 originalHeight = image.shape[0]
50 ratio = max(width, height) / max(originalWidth, originalHeight)
52 newWidth = int(originalWidth * ratio)
53 newHeight = int(originalHeight * ratio)
55 resizedImage = Image.fromarray(image).resize((newWidth, newHeight))
57 deltaW = width - newWidth
58 deltaH = height - newHeight
63 paddedImage = Image.new(resizedImage.mode, (width, height), 0)
64 paddedImage.paste(resizedImage, (left, top))
66 return np.array(paddedImage), top, left
69 def cropToWidth(image: np.ndarray) -> np.ndarray:
71 Crops the image to width while maintaining the center
76 Input image as an array
80 np.ndarray -> Output image that has been croped vertically
84 height, width = image.shape[:2]
86 startY = (height - width) // 2
88 image = image[startY:endY, 0:width]