Upload generate_multiscale_DF2K.py
Browse files
scripts/generate_multiscale_DF2K.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import glob
|
3 |
+
import os
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
|
7 |
+
def main(args):
|
8 |
+
# For DF2K, we consider the following three scales,
|
9 |
+
# and the smallest image whose shortest edge is 400
|
10 |
+
scale_list = [0.75, 0.5, 1 / 3]
|
11 |
+
shortest_edge = 400
|
12 |
+
|
13 |
+
path_list = sorted(glob.glob(os.path.join(args.input, '*')))
|
14 |
+
for path in path_list:
|
15 |
+
print(path)
|
16 |
+
basename = os.path.splitext(os.path.basename(path))[0]
|
17 |
+
|
18 |
+
img = Image.open(path)
|
19 |
+
width, height = img.size
|
20 |
+
for idx, scale in enumerate(scale_list):
|
21 |
+
print(f'\t{scale:.2f}')
|
22 |
+
rlt = img.resize((int(width * scale), int(height * scale)), resample=Image.LANCZOS)
|
23 |
+
rlt.save(os.path.join(args.output, f'{basename}T{idx}.png'))
|
24 |
+
|
25 |
+
# save the smallest image which the shortest edge is 400
|
26 |
+
if width < height:
|
27 |
+
ratio = height / width
|
28 |
+
width = shortest_edge
|
29 |
+
height = int(width * ratio)
|
30 |
+
else:
|
31 |
+
ratio = width / height
|
32 |
+
height = shortest_edge
|
33 |
+
width = int(height * ratio)
|
34 |
+
rlt = img.resize((int(width), int(height)), resample=Image.LANCZOS)
|
35 |
+
rlt.save(os.path.join(args.output, f'{basename}T{idx+1}.png'))
|
36 |
+
|
37 |
+
|
38 |
+
if __name__ == '__main__':
|
39 |
+
"""Generate multi-scale versions for GT images with LANCZOS resampling.
|
40 |
+
It is now used for DF2K dataset (DIV2K + Flickr 2K)
|
41 |
+
"""
|
42 |
+
parser = argparse.ArgumentParser()
|
43 |
+
parser.add_argument('--input', type=str, default='datasets/DF2K/DF2K_HR', help='Input folder')
|
44 |
+
parser.add_argument('--output', type=str, default='datasets/DF2K/DF2K_multiscale', help='Output folder')
|
45 |
+
args = parser.parse_args()
|
46 |
+
|
47 |
+
os.makedirs(args.output, exist_ok=True)
|
48 |
+
main(args)
|