#resize.py
import os
import argparse
from PIL import Image #pip install pillow 安装
parse=argparse.ArgumentParser()
parse.add_argument('--input', type=str, default='./input_img.png',
help='Your input_img like \'example.jpg\', add path info if the img and this file in different directories.')
parse.add_argument('--output', type=str, default='./output_resized.png',
help='Name of your output img like \'output_resized.png\', add path info if you wish your output to be in another directory.')
parse.add_argument('--size', type=str, default='2048x1024',
help='Set your resize scale, use small letter \'x\' to split width and height, example:\'1920x1080\'.')
parse.add_argument('--input_dir',type=str, default='None',
help='Set path to directory in which you wish to resize all imgs. Single img resize function won\'t work if you use this option.')
parse.add_argument('--output_dir', type=str, default='./output_resized',
help='Set absolute path of directory where you wish to save resized imgs. Directory(whether default or defined by you) will be created if it doesn\'t exist.')
args=parse.parse_args()
def resize_one(input_path, output_size, size):
width=int(size.split('x')[0])
height=int(size.split('x')[1])
try:
origin_img=Image.open(input_path).resize((width,height), Image.ANTIALIAS)
except FileNotFoundError:
print('Input img not exists, check your file again. \nOr type \'python resize.py --help\' for more infomation.')
else:
print('Resize img to size: {size_tar} \n'.format(size_tar=size))
target_img=origin_img.save(output_size)
print('Done')
def resize_all(input_dir, size, output_dir):
try:
files = os.listdir(input_dir)
except FileNotFoundError:
print('input_dir not exists, check your directory again.\nOr type \'python resize.py --help\' for more infomation.')
else:
if len(files)==0:
print('No img found in your directory.\nDone.')
else:
saved_path=output_dir
if os.path.exists(saved_path)==False:
try:
print('output_dir not exists, trying to create...')
os.makedirs(saved_path)
except PermissionError:
print('Fail to create output_dir probably due to the permission, launch cmd with administor rights and try again')
else:
print('output_dir created')
width=int(size.split('x')[0])
height=int(size.split('x')[1])
for i in files:
print('Resizing {current_img}...'.format(current_img=i))
img = os.path.join(input_dir, i) #path/1.jpg
img = Image.open(img).resize((width, height),Image.ANTIALIAS)
file_name, file_extend = os.path.splitext(i)
dst = os.path.join(os.path.abspath(saved_path), file_name + '.png')
img.save(dst)
print('All done, imgs saved in : {target_dir}'.format(target_dir=saved_path))
if __name__=='__main__':
if args.input_dir==None:
resize_one(args.input, args.output, args.size)
else:
resize_all(args.input_dir, args.size, args.output_dir)
将resize.py和图片放在同一目录下(自己指定路径也可以),打开命令行:
python resize.py --input origin.png --output resized_img.png --size 1920x1080
或者把resize.py和文件夹放在同一目录下,运行:
python resize.py --input_dir test --size 1920x1080
输出结果会保存在与文件夹同目录的文件夹下
也可以自己决定保存的路径,文件夹如果不存在则会自动创建:
python resize.py --input_dir test --output_dir C:\output_resized --size 1920x1080