-
Notifications
You must be signed in to change notification settings - Fork 4
/
preprocess_images.py
52 lines (40 loc) · 1.59 KB
/
preprocess_images.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
48
49
50
51
52
"""
For every image, center crop and resize to (224,224) and save.
"""
import argparse
from glob import glob
import os
from os.path import exists, join
from PIL import Image, ImageOps
from zipfile import ZipFile
from tqdm import tqdm
def preprocess_images(img_dir, target_dir):
if not exists(target_dir):
os.makedirs(target_dir)
zip_list = glob(join(img_dir, '*.zip'))
broken_file_list = []
for zip_file in sorted(zip_list):
target_zip_dir = zip_file.replace(img_dir, target_dir)[:-len('.zip')]
if not exists(target_zip_dir):
os.makedirs(target_zip_dir)
with ZipFile(zip_file, 'r') as archive:
for img_name in tqdm(archive.namelist(), desc=zip_file.replace(img_dir, '')):
if not img_name.endswith('.png'):
continue
with archive.open(img_name) as file:
try:
im = Image.open(file)
im = ImageOps.fit(im, size=(224, 224))
except Exception as e:
broken_file_list.append(img_name)
continue
im.save(join(target_zip_dir, img_name))
with open(join(target_dir, 'broken_images.txt'), 'w') as f:
for item in broken_file_list:
f.write("{}\n".format(item))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Usage')
parser.add_argument('img_dir', type=str)
parser.add_argument('target_dir', type=str)
args = parser.parse_args()
preprocess_images(args.img_dir, args.target_dir)