Skip to content

Instantly share code, notes, and snippets.

@tomahim
Last active January 20, 2022 08:26
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save tomahim/9ef72befd43f5c106e592425453cb6ae to your computer and use it in GitHub Desktop.
Save tomahim/9ef72befd43f5c106e592425453cb6ae to your computer and use it in GitHub Desktop.
Data augmentation in few lines with skimage
import os
import random
from scipy import ndarray
# image processing library
import skimage as sk
from skimage import transform
from skimage import util
from skimage import io
def random_rotation(image_array: ndarray):
# pick a random degree of rotation between 25% on the left and 25% on the right
random_degree = random.uniform(-25, 25)
return sk.transform.rotate(image_array, random_degree)
def random_noise(image_array: ndarray):
# add random noise to the image
return sk.util.random_noise(image_array)
def horizontal_flip(image_array: ndarray):
# horizontal flip doesn't need skimage, it's easy as flipping the image array of pixels !
return image_array[:, ::-1]
# dictionary of the transformations we defined earlier
available_transformations = {
'rotate': random_rotation,
'noise': random_noise,
'horizontal_flip': horizontal_flip
}
folder_path = 'images/cat'
num_files_desired = 10
# find all files paths from the folder
images = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
num_generated_files = 0
while num_generated_files <= num_files_desired:
# random image from the folder
image_path = random.choice(images)
# read image as an two dimensional array of pixels
image_to_transform = sk.io.imread(image_path)
# random num of transformation to apply
num_transformations_to_apply = random.randint(1, len(available_transformations))
num_transformations = 0
transformed_image = None
while num_transformations <= num_transformations_to_apply:
# random transformation to apply for a single image
key = random.choice(list(available_transformations))
transformed_image = available_transformations[key](image_to_transform)
num_transformations += 1
new_file_path = '%s/augmented_image_%s.jpg' % (folder_path, num_generated_files)
# write image to the disk
io.imsave(new_file_path, transformed_image)
num_generated_files += 1
@JuanjoChiarella
Copy link

all the lines since line 54 must be in the first while.

@whiteDigitalAI
Copy link

Hello Tom,

Thank you.. But it throws an error for some files (Original files are in .JPG format)

The error

"cannot write mode %s as JPEG" % im.mode"

@Amit-k-maurya-16
Copy link

yupp, JuanjoChiarell you are right, otherwise it will go in infinite loop..

@Habki
Copy link

Habki commented Jul 5, 2019

hello there...i need your help for this error...thank you!!!

"Could not find a format to read the specified file " "in mode %r" % mode
ValueError: Could not find a format to read the specified file in mode 'i'

@mohan-aditya05
Copy link

all the lines since line 54 must be in the first while.

true! indentation needs to be given.

@mohammadrafaykhan
Copy link

can anyone share the correct code?

@Faguilar-V
Copy link

AttributeError: module 'skimage' has no attribute 'transform'

I have this issue

@anaves
Copy link

anaves commented May 27, 2020

all the lines since line 54 must be in the first while.

true! indentation needs to be given.

yes! after line 54 must be aligned with the first while.

@manimtechrs
Copy link

how to align all lines with first while ..
`while num_generated_files <= num_files_desired:
# random image from the folder
image_path = random.choice(images)
# read image as an two dimensional array of pixels
image_to_transform = sk.io.imread(image_path)
# random num of transformation to apply
num_transformations_to_apply = random.randint(1, len(available_transformations))

num_transformations = 0
transformed_image = None
while num_transformations <= num_transformations_to_apply:
    # random transformation to apply for a single image
key = random.choice(list(available_transformations))
transformed_image = available_transformations[key](image_to_transform)`

@anaves
Copy link

anaves commented Jun 15, 2020

correct the code and make it available here

@kelingkece
Copy link

how to make the code can augmented the data in all images in a folder instead of randoming?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment