File size: 878 Bytes
8314c30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import torch.nn.functional as F
from PIL import Image

def smart_padding(image, divisor=16):
    """ Pad the image so that its dimensions are divisible by the divisor. """
    h, w = image.shape[-2:]
    pad_h = (divisor - h % divisor) % divisor
    pad_w = (divisor - w % divisor) % divisor
    left = pad_w // 2
    right = pad_w - left
    top = pad_h // 2
    bottom = pad_h - top
    padding = (left, right, top, bottom)
    padded_image = F.pad(image, padding, mode='constant', value=1.0)
    return padded_image, padding

def remove_padding(image, padding):
    """ Remove the padding from the image. """
    left, right, top, bottom = padding
    if right == 0:
        w_end = image.shape[-1]
    else:
        w_end = -right
    if bottom == 0:
        h_end = image.shape[-2]
    else:
        h_end = -bottom
    return image[..., top:h_end, left:w_end]