File size: 2,399 Bytes
eafbf97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""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

    # Crop the center of the image
    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):
    # Convert the numpy array image to PIL Image
    pil_image = Image.fromarray(image)

    # Resize the PIL Image
    resized_image = pil_image.resize((new_width, new_height))

    # Convert the resized PIL Image back to numpy array
    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."""
    # Convert the numpy array image to PIL Image
    # pil_image = Image.fromarray(image)

    # Get the current width and height of the image
    width, height = pil_image.size
    assert new_width > width, f"New width {new_width} is less than the current width {width}."

    # Calculate the padding required
    hor_pad = new_width - width

    # Add padding to the image
    padded_image = add_margin(pil_image, 0, hor_pad, 0, 0, color=color)

    # Convert the padded PIL Image back to numpy array
    # padded_image_np = np.array(padded_image)

    return padded_image