学习日志(十三):读取指定路径下图片并转换保存

import argparse
import os
import glob
import torch
import PIL.Image as image
from tqdm import tqdm

parser = argparse.ArgumentParser()
parser.add_argument('--hr-path',type=str,default='/home/dushuai/word/superpixel_fcn-master/demo/inputs')
parser.add_argument('--lr-path',type=str,default='/home/dushuai/word/superpixel_fcn-master/demo/output')
parser.add_argument('--num',type = int,default=3)
parser.add_argument('--scale',type = int,default=2)
args = parser.parse_args()

if torch.cuda.is_available():
    device = torch.device('cuda')
else:
    device = torch.device('cpu')

if not os.path.exists(args.lr_path):
        os.makedirs(args.lr_path)

def convert2lr(args):
    with tqdm(total=args.num) as t:
        for image_relative_name in os.listdir(args.hr_path):
            image_path = os.path.join(args.hr_path,image_relative_name)
            hr = image.open(image_path).convert('RGB')
            hr_width = (hr.width//args.scale)*args.scale
            hr_height = (hr.height//args.scale)*args.scale
            hr = hr.resize((hr_width,hr_height),resample=image.BICUBIC)
            lr = hr.resize((hr_width//args.scale,hr_height//args.scale),resample=image.BICUBIC)
            lr = lr.resize((lr.width*args.scale,lr.height*args.scale),resample=image.BICUBIC)
            lr.save('/home/dushuai/word/superpixel_fcn-master/demo/output/{}'.format(image_relative_name))
            t.update()

if __name__ == '__main__':
    convert2lr(args)

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44825185/article/details/109442145