-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize.py
47 lines (43 loc) · 1.4 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from PIL import Image
import os
import pdb
def resize_image(image):
"""
resized COCO image to 224x224 (VGG)
"""
width, height = image.size
if width > height:
left = (width - height) / 2
right = width - left
top = 0
bottom = height
else:
top = (height - width) / 2
bottom = height - top
left = 0
right = width
image = image.crop((left, top, right, bottom))
image = image.resize([224, 224], Image.ANTIALIAS)
return image
def main():
"""
resized all COCO images
"""
splits = ['train','val']
for split in splits:
folder = '/media/sam/112695C05B8509FA/data/coco_caption/raw-data/%s2014' %split
resized_folder = './image/%s2014_resized/' %split
if not os.path.exists(resized_folder):
os.makedirs(resized_folder)
print 'Start resizing %s images.' %split
image_files = os.listdir(folder)
num_images = len(image_files)
for i, image_file in enumerate(image_files):
file_path = os.path.join(folder, image_file)
image = Image.open(open(file_path, 'rb'))
image = resize_image(image)
image.save(os.path.join(resized_folder, image_file), image.format)
if i % 100 == 0:
print 'Resized images: %d/%d' %(i, num_images)
if __name__ == '__main__':
main()