-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathresize.py
More file actions
executable file
·57 lines (45 loc) · 1.35 KB
/
resize.py
File metadata and controls
executable file
·57 lines (45 loc) · 1.35 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
#!/usr/bin/env python
# encoding: utf-8
"""
Usage:
# Script to resize all files in current directory,
saving new .jpg images in a new folder.
# eg. python resize.py
"""
import cv2
import glob
import os
import sys
import argparse
max_size = 512
folder = 'resized'
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--max_size", type=int, default=max_size,
help="the maximal image width/height")
parser.add_argument("-d", "--dest", default=folder,
help="destitation folder for resized images")
args = parser.parse_args()
max_size, folder = args.max_size, args.dest
def resize():
# find all jpg images
imgs = glob.glob('*.jpg')
print('Found files:')
print(imgs)
print('Resizing all images be %d pixels wide or height' % max_size)
if not os.path.exists(folder):
os.makedirs(folder)
# Iterate through resizing and saving
for img in imgs:
pic = cv2.imread(img, cv2.IMREAD_UNCHANGED)
ht, wd = pic.shape[0], pic.shape[1]
print(img, wd, ht)
if wd >= ht:
width = max_size
height = int(width * ht / wd)
else:
height = max_size
width = int(height * wd / ht)
pic = cv2.resize(pic, (width, height))
cv2.imwrite(folder + '/' + img, pic)
if __name__ == "__main__":
resize()