Last active
February 13, 2019 05:16
-
-
Save yongjun823/d761e734e2a7929894622132c7a17d75 to your computer and use it in GitHub Desktop.
image resize & random crop , crop --> resize, SR train data
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
import random | |
import os | |
from tqdm import tqdm | |
from PIL import Image, ImageOps | |
from concurrent.futures import ThreadPoolExecutor | |
original_path = './original/' | |
resize_path = './resize/' | |
data_path = './data/' | |
pbar = None | |
def split_name(name): | |
return name.split('/')[-1].split('.')[0] | |
def resize_image(image_path): | |
img = Image.open(image_path) | |
temp_name = split_name(image_path) | |
width, height = img.size | |
for i in range(16): | |
t_w = random.randrange(0, width - 384) | |
t_h = random.randrange(0, height - 384) | |
fit_img = img.crop((t_w, t_h, t_w + 384, t_h + 384)) | |
fit_img.save('{}{}_{}.jpg'.format(original_path, temp_name, i)) | |
fit_img = ImageOps.fit(fit_img, (96, 96), Image.BICUBIC) | |
fit_img.save('{}{}_{}.jpg'.format(resize_path, temp_name, i)) | |
pbar.update(1) | |
if __name__ == "__main__": | |
temp_arr = [os.path.join(data_path, x) for x in os.listdir(data_path)] | |
print('data length: ', len(temp_arr)) | |
pbar = tqdm(total=len(temp_arr)) | |
with ThreadPoolExecutor() as executor: | |
executor.map(resize_image, temp_arr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment