__author__ = 'chunk' import os, sys from PIL import Image from ..common import * import random import cv2 import numpy as np base_dir = '/data/hadoop/ImageNet/ILSVRC/ILSVRC2013_DET_val/' category = 'Test' def crop_Test(): for path, subdirs, files in os.walk(os.path.join(base_dir, category)): for name in files: image = os.path.join(path, name) print image # try: # im = Image.open(image) # w, h = im.size # if w < 300 or h < 300: # continue # left, upper = random.randint(0, w - 300), random.randint(0, h - 300) # im = im.crop((left, upper, left + 300, upper + 300)) # im.save(os.path.join(base_dir, category + '_crop_pil', name)) # except: # pass try: img = cv2.imread(image, cv2.CV_LOAD_IMAGE_UNCHANGED) h, w = img.shape[:2] left, upper = random.randint(0, w - 300), random.randint(0, h - 300) img_crop = img[upper:upper + 300, left:left + 300] cv2.imwrite(os.path.join(base_dir, category + '_crop_cv', name),img_crop) except Exception as e: print '[EXCPT]',e pass if __name__ == '__main__': timer = Timer() timer.mark() crop_Test() timer.report() pass