#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import os
from PIL import Image

CNN_IMAGE_SIZE = 32
postfix = str(CNN_IMAGE_SIZE) + 'x' + str(CNN_IMAGE_SIZE)

TRAIN_DIR = 'flickr_logos_27_dataset'
TRAIN_IMAGE_DIR = os.path.join(TRAIN_DIR, 'flickr_logos_27_dataset_images')
CROPPED_IMAGE_DIR = os.path.join(
    TRAIN_DIR,
    '_'.join(['flickr_logos_27_dataset_cropped', postfix, 'images']))


def main():
    annot_train = np.loadtxt(
        os.path.join(TRAIN_DIR,
                     'flickr_logos_27_dataset_training_set_annotation.txt'),
        dtype='a')
    print('train_annotation: %d, %d ' % (annot_train.shape))

    #
    # crop logo from an image
    #

    if not os.path.exists(CROPPED_IMAGE_DIR):
        os.makedirs(CROPPED_IMAGE_DIR)

    for annot in annot_train:
        fn = annot[0].decode('utf-8')
        x1, y1, x2, y2 = list(map(int, annot[3:]))
        im = Image.open(os.path.join(TRAIN_IMAGE_DIR, fn))

        cx, cy = (x1 + x1) // 2, (y1 + y2) // 2
        cropped_im = im.crop(
            (cx - (CNN_IMAGE_SIZE // 2), cy - (CNN_IMAGE_SIZE // 2),
             cx + (CNN_IMAGE_SIZE // 2), cy + (CNN_IMAGE_SIZE // 2)))

        _, ext = os.path.splitext(fn)
        cropped_fn = '_'.join([fn.split('.')[0], postfix]) + ext
        cropped_im.save(os.path.join(CROPPED_IMAGE_DIR, cropped_fn))

    # check
    org_imgs = [img for img in os.listdir(TRAIN_IMAGE_DIR)]
    cropped_imgs = [img for img in os.listdir(CROPPED_IMAGE_DIR)]
    print('original: %d' % (len(org_imgs)))
    print('cropped: %d' % (len(cropped_imgs)))


if __name__ == '__main__':
    main()

@@ -89,16 +89,52 @@
 rt Image
+%0Afrom collections import defaultdict
 %0A%0ACNN_IM
@@ -353,21 +353,16 @@
 th.join(
-%0A    
 TRAIN_DI
@@ -368,26 +368,45 @@
 IR,%0A    
-'_'.join(%5B
+                             
 'flickr_
@@ -433,30 +433,16 @@
 pped
-', postfix, '
+_
 images'
-%5D)
 )%0A%0A%0A
@@ -810,397 +810,548 @@
     
-for annot in annot_train:%0A        fn = annot%5B0%5D.decode('utf-8')%0A        x1, y1, x2, y2 = list(map(int, annot%5B3:%5D))%0A        im = Image.open(os.path.join(TRAIN_IMAGE_DIR, fn))%0A%0A        cx, cy = (x1 + x1) // 2, (y1 + y2) // 2%0A        cropped_im = im.crop(%0A            (cx - (CNN_IMAGE_SIZE // 2), cy - (CNN_IMAGE_SIZE // 2),%0A             cx + (CNN_IMAGE_SIZE // 2), cy + (CNN_IMAGE_SIZE // 2)
+# Multiple images are cropped from same file.%0A    crop_per_files = defaultdict(int)%0A    for annot in annot_train:%0A        fn = annot%5B0%5D.decode('utf-8')%0A        class_name = annot%5B1%5D.decode('utf-8')%0A        train_subset_class = annot%5B2%5D.decode('utf-8')%0A%0A        crop_per_files%5Bfn%5D += 1%0A        x1, y1, x2, y2 = list(map(int, annot%5B3:%5D))%0A        if (x2 - x1) %3C= 0 or (y2 - y1) %3C= 0:%0A            print('Skipping:', fn)%0A            continue%0A        im = Image.open(os.path.join(TRAIN_IMAGE_DIR, fn))%0A        cropped_im = im.crop((x1, y1, x2, y2
 ))%0A%0A
@@ -1418,16 +1418,29 @@
 _'.join(
+%0A            
 %5Bfn.spli
@@ -1450,23 +1450,84 @@
 .')%5B0%5D, 
-postfix
+class_name, train_subset_class,%0A             str(crop_per_files%5Bfn%5D)
 %5D) + ext

Change cropped file name