Spaces:
Runtime error
Runtime error
File size: 1,725 Bytes
810c8ea |
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 |
import argparse
import glob
import os
from PIL import Image
def main(args):
# For DF2K, we consider the following three scales,
# and the smallest image whose shortest edge is 400
scale_list = [0.75, 0.5, 1 / 3]
shortest_edge = 400
path_list = sorted(glob.glob(os.path.join(args.input, '*')))
for path in path_list:
print(path)
basename = os.path.splitext(os.path.basename(path))[0]
img = Image.open(path)
width, height = img.size
for idx, scale in enumerate(scale_list):
print(f'\t{scale:.2f}')
rlt = img.resize((int(width * scale), int(height * scale)), resample=Image.LANCZOS)
rlt.save(os.path.join(args.output, f'{basename}T{idx}.png'))
# save the smallest image which the shortest edge is 400
if width < height:
ratio = height / width
width = shortest_edge
height = int(width * ratio)
else:
ratio = width / height
height = shortest_edge
width = int(height * ratio)
rlt = img.resize((int(width), int(height)), resample=Image.LANCZOS)
rlt.save(os.path.join(args.output, f'{basename}T{idx+1}.png'))
if __name__ == '__main__':
"""Generate multi-scale versions for GT images with LANCZOS resampling.
It is now used for DF2K dataset (DIV2K + Flickr 2K)
"""
parser = argparse.ArgumentParser()
parser.add_argument('--input', type=str, default='datasets/DF2K/DF2K_HR', help='Input folder')
parser.add_argument('--output', type=str, default='datasets/DF2K/DF2K_multiscale', help='Output folder')
args = parser.parse_args()
os.makedirs(args.output, exist_ok=True)
main(args)
|