Real-ESRGAN
48 строк · 1.7 Кб
1import argparse2import glob3import os4from PIL import Image5
6
7def main(args):8# For DF2K, we consider the following three scales,9# and the smallest image whose shortest edge is 40010scale_list = [0.75, 0.5, 1 / 3]11shortest_edge = 40012
13path_list = sorted(glob.glob(os.path.join(args.input, '*')))14for path in path_list:15print(path)16basename = os.path.splitext(os.path.basename(path))[0]17
18img = Image.open(path)19width, height = img.size20for idx, scale in enumerate(scale_list):21print(f'\t{scale:.2f}')22rlt = img.resize((int(width * scale), int(height * scale)), resample=Image.LANCZOS)23rlt.save(os.path.join(args.output, f'{basename}T{idx}.png'))24
25# save the smallest image which the shortest edge is 40026if width < height:27ratio = height / width28width = shortest_edge29height = int(width * ratio)30else:31ratio = width / height32height = shortest_edge33width = int(height * ratio)34rlt = img.resize((int(width), int(height)), resample=Image.LANCZOS)35rlt.save(os.path.join(args.output, f'{basename}T{idx+1}.png'))36
37
38if __name__ == '__main__':39"""Generate multi-scale versions for GT images with LANCZOS resampling.40It is now used for DF2K dataset (DIV2K + Flickr 2K)
41"""
42parser = argparse.ArgumentParser()43parser.add_argument('--input', type=str, default='datasets/DF2K/DF2K_HR', help='Input folder')44parser.add_argument('--output', type=str, default='datasets/DF2K/DF2K_multiscale', help='Output folder')45args = parser.parse_args()46
47os.makedirs(args.output, exist_ok=True)48main(args)49