A subroutine to implement padding and resizing. This will resize the image to fit fully within the input size, and pads the remaining bottom-right portions with the value provided. :param image: The PIL image object :pad_color: The RGB values to use f
(image, pad_color=(0, 0, 0))
| 118 | """ |
| 119 | |
| 120 | def resize_pad(image, pad_color=(0, 0, 0)): |
| 121 | """ |
| 122 | A subroutine to implement padding and resizing. This will resize the image to fit fully within the input |
| 123 | size, and pads the remaining bottom-right portions with the value provided. |
| 124 | :param image: The PIL image object |
| 125 | :pad_color: The RGB values to use for the padded area. Default: Black/Zeros. |
| 126 | :return: Two values: The PIL image object already padded and cropped, and the resize scale used. |
| 127 | """ |
| 128 | width, height = image.size |
| 129 | width_scale = width / self.width |
| 130 | height_scale = height / self.height |
| 131 | scale = 1.0 / max(width_scale, height_scale) |
| 132 | image = image.resize((round(width * scale), round(height * scale)), resample=Image.BILINEAR) |
| 133 | pad = Image.new("RGB", (self.width, self.height)) |
| 134 | pad.paste(pad_color, [0, 0, self.width, self.height]) |
| 135 | pad.paste(image) |
| 136 | return pad, scale |
| 137 | |
| 138 | scale = None |
| 139 | image = Image.open(image_path) |