Created
November 17, 2018 08:09
-
-
Save yongjun823/fce95ceadabf71a826248e5268c052fa to your computer and use it in GitHub Desktop.
python image random crop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image, ImageOps | |
from random import randrange | |
from tqdm import tqdm | |
import os | |
target_size = 448 | |
original_path = 'original' | |
resize_path = 'resize' | |
folder_arr = ['1262768'] | |
def random_crop(folder, image_name): | |
img1 = Image.open(folder + '/' + image_name) | |
img_size = img1.size | |
x_max = img_size[0] - target_size | |
y_max = img_size[1] - target_size | |
for i in range(10): | |
random_x = randrange(0, x_max//2 + 1) * 2 | |
random_y = randrange(0, y_max//2 + 1) * 2 | |
area = (random_x, random_y, random_x + target_size, random_y + target_size) | |
c_img = img1.crop(area) | |
fit_img_h = ImageOps.fit(c_img, (224, 224), Image.ANTIALIAS) | |
fit_img_h.save('{}/{}_{}_{}'.format(original_path, folder, i, image_name)) | |
fit_img_l = ImageOps.fit(c_img, (56, 56), Image.ANTIALIAS) | |
fit_img_l.save('{}/{}_{}_{}'.format(resize_path, folder, i, image_name)) | |
for folder in folder_arr: | |
img_names = os.listdir(folder) | |
for name in tqdm(img_names): | |
random_crop(folder, name) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment