Skip to content

Instantly share code, notes, and snippets.

@tbmreza
Created May 4, 2019 07:07
Show Gist options
  • Save tbmreza/d355cfa7f6b0e7ebafd264d37c026d72 to your computer and use it in GitHub Desktop.
Save tbmreza/d355cfa7f6b0e7ebafd264d37c026d72 to your computer and use it in GitHub Desktop.
Image augmentation using imgaug that loads JPG files in working directory and saves to result folder.
# Modified from a stackoverflow answer. https://stackoverflow.com/a/52147997
import imgaug as ia
from imgaug import augmenters as iaa
import numpy as np
import imageio
import os
number_of_output = 4
result_folder = 'imgaug/'
counter = 1
jpg_files = [jpg_file for jpg_file in os.listdir() if '.jpg' in jpg_file]
for dir in jpg_files:
os.makedirs(result_folder+str(counter), exist_ok=True)
img = imageio.imread(dir)
images = np.array(
[img for _ in range(number_of_output)], dtype=np.uint8)
seq = iaa.Sequential(
[
# Customize manipulation here.
# See documentation. https://github.com/aleju/imgaug#overview-of-most-augmenters
iaa.Fliplr(0.4),
iaa.Flipud(0.4),
iaa.Crop(percent=(0, 0.1)),
iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.5))),
iaa.ContrastNormalization((0.75, 1.5)),
iaa.AdditiveGaussianNoise(
loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
iaa.Multiply((0.8, 1.2), per_channel=0.2),
],
random_order=True)
images_aug = seq.augment_images(images)
for i in range(number_of_output):
imageio.imwrite(result_folder+str(counter)+'/'+str(i)+'.jpg', images_aug[i])
counter = counter + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment