aleju / imgaug

Image augmentation for machine learning experiments.
http://imgaug.readthedocs.io
MIT License
14.28k stars 2.43k forks source link

Locally saving augmented images #258

Open drbrown7 opened 5 years ago

drbrown7 commented 5 years ago

I might have missed something in the documentation, or just don't have enough understanding of Python, but I was wondering if there is a way to save augmented images to a local folder on your PC. I am working on a project where I need to pull images, augment them, then resave them for future use and I was just wondering if that's possible.

aleju commented 5 years ago

imgaug returns images as numpy arrays. Any library that saves numpy image arrays is suitable. imageio or cv2 are common choices. This should work:

import numpy as np
import imageio
from imgaug import augmenters as iaa

# random example image
image = np.random.randint(0, 255, size=(64, 64, 3)).astype(np.uint8)

# augment 16 times the example image
images_aug = iaa.Affine(rotate=(-45, 45)).augment_images([image] * 16)

# iterate over every example image and save it to 0.jpg, 1.jpg, 2.jpg, ...
for i, image_aug in enumerate(images_aug):
    imageio.imwrite("%d.jpg" % (i,), image_aug)
drbrown7 commented 5 years ago

I tried using this code, and while it works properly with a random image, it does not work with an image that I pull locally. local save issue This image show the code I am using, along with the local image I pulled and all of the pictures that were saved. As you can see they are saved into little slivers of images and not anything useful. As a note I originally tried imageio.imwrite instead of cv2.imwrite but the results were exactly the same.

aleju commented 5 years ago

Change the line

images_aug = iaa.Affine(rotate=(-45, 45)).augment_images([image[0]]*10)

to

images_aug = iaa.Affine(rotate=(-45, 45)).augment_images(images)

and try again. Should work then.

cassyay commented 4 years ago

I am using this code and the augmented images look fine on Jupyter, but I am getting the same flat images, and hundreds of copies saved locally:

path = 'C:\\path to folder containing folders of images'

ia.seed(2)

seq = iaa.Sequential([
    iaa.Fliplr(0.5),
    iaa.Crop(percent=(0, 0.1)),
    iaa.Affine(rotate=(-25,25))
], random_order=True)

for folder in os.listdir(path):
    for i in os.listdir(path + '\\' + folder):
        img = imageio.imread(path + '\\' + folder + '\\' + i)
        print('Original:')
        ia.imshow(img)
        img_aug = seq.augment_image(img)
        print('Augmented:')
        ia.imshow(img_aug)
for im, im_aug in enumerate(img_aug):
    imageio.imwrite(os.path.join(path, path + '\\' + folder + '\\' + folder + "%06d.png" % (im,)), im_aug)

I have my code as .augment_image(img) because when I use augment_images(img) plural my images are returned as black in Jupyter.

aleju commented 4 years ago

You are iterating over enumerate(img_aug), which is not a list of augmented images, but only the one last image that you loaded in your loop (i.e. you are saving each row of the last image as its own image). Something like that should work:

path = 'C:\\path to folder containing folders of images'

ia.seed(2)

seq = iaa.Sequential([
    iaa.Fliplr(0.5),
    iaa.Crop(percent=(0, 0.1)),
    iaa.Affine(rotate=(-25,25))
], random_order=True)

for folder in os.listdir(path):
    i = 0
    for fname in os.listdir(path + '\\' + folder):
        img = imageio.imread(path + '\\' + folder + '\\' + fname)
        print('Original:')
        ia.imshow(img)
        img_aug = seq.augment_image(img)
        print('Augmented:')
        ia.imshow(img_aug)

        imageio.imwrite(os.path.join(path, path + '\\' + folder + '\\' + folder + "%06d.png" % (i,)), img_aug)
        i += 1
cassyay commented 4 years ago

That worked, thanks!

TemitopeOladokun commented 4 years ago

To specify the path in your local computer or colab. for i, image_aug in enumerate(newimg): imageio.imwrite(os.path.join(path, "%08d.jpg" % (i,)), image_aug)