img_process.py 1.63 KB
import os, sys
from PIL import Image

def make_img(filename):
    try:
        with Image.open(filename + '.png') as im:
            print("File ("+filename +".png)")

            if im.width<im.height :
                crop_size = (im.height-im.width)/2
                area = (0, int(crop_size), im.width, int(crop_size)+im.width)

            elif im.width>im.height :
                crop_size = (im.width-im.height) / 2
                area = (int(crop_size), 0, int(crop_size)+im.height, im.height)

            else :
                area = (0, 0, im.width , im.height)

            cropped_img = im.crop(area)
            cropped_img.thumbnail((32, 32))
            cropped_img.save(filename + "_0.png")

            im2 = cropped_img.rotate(90)
            im2.save(filename + "_90.png")

            im3 = cropped_img.rotate(180)
            im3.save(filename + "_180.png")

            im4 = cropped_img.rotate(270)
            im4.save(filename + "_270.png")

            im_transpose = cropped_img.transpose(Image.FLIP_LEFT_RIGHT)
            im5 = im_transpose
            im5.save(filename + "_transpose_0.png")

            im6 = im_transpose.rotate(90)
            im6.save(filename + "_transpose_90.png")

            im7 = im_transpose.rotate(180)
            im7.save(filename + "_transpose_180.png")

            im8 = im_transpose.rotate(270)
            im8.save(filename + "_transpose_270.png")

            os.remove(filename+'.png')

    except IOError:
        print("No file ("+filename +".png)")



for i in range(0,1000):
        filename =  str(i)
        make_img(filename)
        filename = '0' + str(i)
        make_img(filename)