-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRESIZE_IMGS.py
More file actions
executable file
·60 lines (45 loc) · 1.82 KB
/
RESIZE_IMGS.py
File metadata and controls
executable file
·60 lines (45 loc) · 1.82 KB
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
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
# @Author: rhys
# @Date: 2016-11-06 14:19:59
# @Last Modified by: stoonejames
# @Last Modified time: 2016-12-10 09:53:39
from PIL import Image
import imghdr
import os
import sys
import argparse
DEFAULT_SIZE = (400, 250)
DEFUALT_DIR = os.getcwd()
parser = argparse.ArgumentParser(
description="Resize images to specified size in directory you specified")
parser.add_argument(
"-d", "--directory", help="the directory, default current directory", default=DEFUALT_DIR)
parser.add_argument("-W", "--width", type=int,
help="the width you want to resize to, default 400", default=DEFAULT_SIZE[0])
parser.add_argument("-H", "--height", type=int,
help="the height you want to resize to, default 250", default=DEFAULT_SIZE[1])
args = parser.parse_args()
RESULT_FOLDER = r'RESIZED_TO_({}_{})_PICs'.format(args.width, args.height)
def resize_img(img_name):
splited_img_name = os.path.splitext(img_name)
img = Image.open(img_name)
print 'imgname', img.filename
if img.size == (args.width, args.height):
return
resized_img = img.resize((args.width, args.height), Image.ANTIALIAS)
resized_name = splited_img_name[0]+"_resized" + splited_img_name[1]
resized_img.save(
RESULT_FOLDER + os.path.sep + resized_name)
print("Resize {} ====> {}".format(
img.filename, RESULT_FOLDER + os.path.sep + resized_name))
if not os.path.exists(args.directory):
sys.exit("-Path {} does not exists".format(args.directory))
abs_resulted_dir = os.path.join(args.directory, RESULT_FOLDER)
if not os.path.exists(abs_resulted_dir):
os.mkdir(abs_resulted_dir)
os.chdir(args.directory)
imgs = filter(lambda it: os.path.isfile(
it) and imghdr.what(it), os.listdir(args.directory))
for img in imgs:
resize_img(img)