crop.py 812 Bytes
__author__ = 'chunk'

import os, sys
from PIL import Image
from common import *
import random

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)
            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', name))



if __name__ == '__main__':
    timer = Timer()

    timer.mark()
    crop_Test()
    timer.report()

    pass