File size: 1,184 Bytes
4a10914
 
2620eb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a10914
 
 
 
 
 
dcc6440
 
4a10914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from torchvision import transforms

def normalize_lab(L, ab):
    """
    Normalize the L and ab channels of an image in Lab color space.
    (Even though ab channels are in [-128, 127] range, we divide them by 110 because higher values are very rare.
    This makes the distribution closer to [-1, 1] in most cases.)
    """
    L = L / 50. - 1.
    ab = ab / 110.
    return L, ab

def denormalize_lab(L, ab):
    """
    Denormalize the L and ab channels of an image in Lab color space.
    (reverse of normalize_lab function)
    """
    L = (L + 1) * 50.
    ab = ab * 110.
    return L, ab

def decide_size(image):
    height = image.size[1]
    width = image.size[0]

    new_height = 256
    new_width = 256

    while new_height < height:
        new_height *= 2
    while new_width < width:
        new_width *= 2

    return new_height, new_width

def pad_image(image):
    height = image.size[1]
    width = image.size[0]

    new_height, new_width = decide_size(image)

    pad_height = new_height - height
    pad_width = new_width - width

    padding = (0, 0, pad_width, pad_height)

    image = transforms.Pad(padding, padding_mode='reflect')(image)

    return image