-
Notifications
You must be signed in to change notification settings - Fork 0
/
bicubic.py
71 lines (63 loc) · 2.79 KB
/
bicubic.py
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
"""
This python script is for converting high-resolution images to low-resolution
bicubic images.
USAGE EXAMPLE:
python bicubic.py --path ../input/Set14/original ../input/Set5/original --scale-factor 2x
"""
from PIL import Image
import glob as glob
import os
import argparse
# Construct the argument parser and parse the command line arguments.
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--path', default='../input/Set14/original',
# nargs : Number of times the argument can be used.
nargs='+',
help='path to the high-res images to convert to low-res')
parser.add_argument('-s', '--scale-factor', dest='scale_factor', default='2x',
help='make low-res by how much factor',
# choices : Limit values to a specific set of choices.
choices=['2x', '3x', '4x'])
args = vars(parser.parse_args())
paths = args['path']
images = []
for path in paths:
images.extend(glob.glob(f"{path}/*.png"))
print(len(images))
# Select scaling-factor and set up directories according to that.
if args['scale_factor'] == '2x':
scale_factor = 0.5
os.makedirs('../input/test_bicubic_rgb_2x', exist_ok=True)
save_path_lr = '../input/test_bicubic_rgb_2x'
os.makedirs('../input/test_hr', exist_ok=True)
save_path_hr = '../input/test_hr'
if args['scale_factor'] == '3x':
scale_factor = 0.333
os.makedirs('../input/test_bicubic_rgb_3x', exist_ok=True)
os.makedirs('../input/test_hr', exist_ok=True)
save_path_lr = '../input/test_bicubic_rgb_3x'
save_path_hr = '../input/test_hr'
if args['scale_factor'] == '4x':
scale_factor = 0.25
# os.makedirs : Recursive directory creation function.
# If exist_ok is False (the default),
# a FileExistsError is raised if the target directory already exists.
os.makedirs('../input/test_bicubic_rgb_4x', exist_ok=True)
os.makedirs('../input/test_hr', exist_ok=True)
save_path_lr = '../input/test_bicubic_rgb_4x'
save_path_hr = '../input/test_hr'
print(f"Scaling factor: {args['scale_factor']}")
print(f"Low resolution images save path: {save_path_lr}")
for image in images:
orig_img = Image.open(image)
image_name = image.split(os.path.sep)[-1]
w, h = orig_img.size[:]
print(f"Original image dimensions: {w}, {h}")
# original images save to '../input/test_hr'
orig_img.save(f"{save_path_hr}/{image_name}")
low_res_img = orig_img.resize(
(int(w*scale_factor), int(h*scale_factor)), Image.BICUBIC)
# Upscale using BICUBIC.
high_res_upscale = low_res_img.resize((w, h), Image.BICUBIC)
# blurred images save to '../input/test_bicubic_rgb_?x', where ? = 2, 3, 4.
high_res_upscale.save(f"{save_path_lr}/{image_name}")