| """Image operations.""" |
| from copy import deepcopy |
| from PIL import Image |
| import numpy as np |
|
|
|
|
| def center_crop(im: Image): |
| width, height = im.size |
| new_width = width if width < height else height |
| new_height = height if height < width else width |
|
|
| left = (width - new_width)/2 |
| top = (height - new_height)/2 |
| right = (width + new_width)/2 |
| bottom = (height + new_height)/2 |
|
|
| |
| im = im.crop((left, top, right, bottom)) |
| |
| return im |
|
|
|
|
| def pad_to_square(im: Image, color=(0, 0, 0)): |
| im = deepcopy(im) |
| width, height = im.size |
|
|
| vert_pad = (max(width, height) - height) // 2 |
| hor_pad = (max(width, height) - width) // 2 |
| |
| if len(im.mode) == 3: |
| color = (0, 0, 0) |
| elif len(im.mode) == 1: |
| color = 0 |
| else: |
| raise ValueError(f"Image mode not supported. Image has {im.mode} channels.") |
| |
| return add_margin(im, vert_pad, hor_pad, vert_pad, hor_pad, color=color) |
|
|
|
|
| def add_margin(pil_img, top, right, bottom, left, color=(0, 0, 0)): |
| """Ref: https://note.nkmk.me/en/python-pillow-add-margin-expand-canvas/""" |
| width, height = pil_img.size |
| new_width = width + right + left |
| new_height = height + top + bottom |
| result = Image.new(pil_img.mode, (new_width, new_height), color) |
| result.paste(pil_img, (left, top)) |
| return result |
|
|
|
|
| def resize_image(image, new_height, new_width): |
| |
| pil_image = Image.fromarray(image) |
|
|
| |
| resized_image = pil_image.resize((new_width, new_height)) |
|
|
| |
| resized_image_np = np.array(resized_image) |
|
|
| return resized_image_np |
|
|
|
|
| def pad_to_width(pil_image, new_width, color=(0, 0, 0)): |
| """Pad the image to the specified width.""" |
| |
| |
|
|
| |
| width, height = pil_image.size |
| assert new_width > width, f"New width {new_width} is less than the current width {width}." |
|
|
| |
| hor_pad = new_width - width |
|
|
| |
| padded_image = add_margin(pil_image, 0, hor_pad, 0, 0, color=color) |
|
|
| |
| |
|
|
| return padded_image |
|
|