File size: 1,765 Bytes
5f3481c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from PIL import Image
from tqdm import tqdm
import numpy as np  
import argparse
import glob
import pdb
import os

parser = argparse.ArgumentParser(description="resize image using bicubic upsampling")
parser.add_argument("--in_dir", default='/mnt/session_space/home/lingzzha/EgoHOS/data/train/label_foreground_512', type=str)
parser.add_argument("--out_dir", default='/mnt/session_space/home/lingzzha/EgoHOS/data/train/label_foreground_512_pad', type=str)
parser.add_argument("--pad_info_dir", default='/mnt/session_space/home/lingzzha/EgoHOS/data/train/label_foreground_512_pad_info', type=str)
parser.add_argument("--size", default=512, type=int)
args = parser.parse_args()

os.makedirs(args.out_dir, exist_ok=True)
os.makedirs(args.pad_info_dir, exist_ok=True)

i = 0
for img_file in tqdm(glob.glob(args.in_dir+'/*')):

    fname = img_file.split('/')[-1]

    img = np.array(Image.open(img_file))
    w, h = img.shape[0], img.shape[1]
    
    if len(img.shape) == 3:
        img_pad = np.zeros((args.size, args.size, img.shape[2]))
    else:
        img_pad = np.zeros((args.size, args.size))

    pad_info = np.zeros((1,2))

    if w < h: 
        if len(img.shape) == 3:
            img_pad[args.size - w:, :, :] = img
        else:
            img_pad[args.size - w:, :] = img
        pad_info[0, 0] = 0
        pad_info[0, 1] = w
    else:
        if len(img.shape) == 3:
            img_pad[:, :h, :] = img
        else:
            img_pad[:, :h] = img
        pad_info[0, 0] = 1
        pad_info[0, 1] = h
    
    img_pad = Image.fromarray(img_pad.astype('uint8'))
    img_pad.save(os.path.join(args.out_dir, fname))
    
    np.save(os.path.join(args.pad_info_dir, str(fname.split('.')[0])+'.npy'), pad_info)

    i += 1

    # pdb.set_trace()