Created
March 21, 2022 19:11
-
-
Save Norod/bf9788fa3fbc09a0557372e779c4d966 to your computer and use it in GitHub Desktop.
Go over a folder of images and reduce the quality and/or add noise to some of them (Useful for making a Pix2Pix model more resilient)
This file contains 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
# crapify_images.py | |
# Go over a folder of images and reduce the quality and/or add noise to some of them (Useful for making a Pix2Pix model more resilient) | |
# @Norod78 | |
import skimage | |
import skimage.io | |
import skimage.io._plugins.pil_plugin as pp | |
import numpy as np | |
from PIL import Image | |
import glob | |
import os | |
number_of_gen_cycles_for_noise = 10 | |
number_of_gen_cycles_for_resize = 9 | |
index = 0 | |
image_output_size = (1024, 1024) | |
input_folder= "./train_data/ffhq" | |
#input_folder= "./test_data/ffhq" | |
input_image_files = glob.glob(os.path.join(input_folder, "*.jpg")) | |
for input_image_file in input_image_files: | |
index = index + 1 | |
imagename_w_ext = os.path.basename(input_image_file) | |
image_name, image_extension = os.path.splitext(imagename_w_ext) | |
if ((index % number_of_gen_cycles_for_resize) == 0): | |
inputImage = Image.open(input_image_file) | |
im96x96 = inputImage.resize((96, 96), resample=Image.NEAREST) | |
inputImage = im96x96.resize(image_output_size, resample=Image.NEAREST) | |
inputImage.save(input_image_file) | |
print("Crapified (resize) \"" + image_name + "\"") | |
if ((index % number_of_gen_cycles_for_noise) == 0): | |
inputImage = Image.open(input_image_file) | |
inputImage = pp.ndarray_to_pil(skimage.util.random_noise(pp.pil_to_ndarray(inputImage), mode="poisson")) | |
inputImage.save(input_image_file) | |
print("Crapified (poisson noise) \"" + image_name + "\"") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment