-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresize.py
30 lines (26 loc) · 833 Bytes
/
resize.py
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
import cv2
import argparse
import os
import numpy
import sys
os.mkdir("resized")
for imgpath in sys.argv:
target_shape = (256, 256)
print imgpath
if imgpath.find('.jpg') == -1:
continue
img = cv2.imread(imgpath)
height, width, depth = img.shape
output_side_length=256
new_height = output_side_length
new_width = output_side_length
if height > width:
new_height = output_side_length * height / width
else:
new_width = output_side_length * width / height
resized_img = cv2.resize(img, (new_width, new_height))
height_offset = (new_height - output_side_length) / 2
width_offset = (new_width - output_side_length) / 2
cropped_img = resized_img[height_offset:height_offset + output_side_length,
width_offset:width_offset + output_side_length]
cv2.imwrite("resized/"+imgpath, cropped_img)