Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locally saving augmented images #258

Open
drbrown7 opened this issue Feb 10, 2019 · 7 comments
Open

Locally saving augmented images #258

drbrown7 opened this issue Feb 10, 2019 · 7 comments

Comments

@drbrown7
Copy link

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
Copy link
Owner

aleju commented Feb 13, 2019

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
Copy link
Author

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
Copy link
Owner

aleju commented Feb 16, 2019

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
Copy link

cassyay commented Jan 10, 2020

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
Copy link
Owner

aleju commented Jan 10, 2020

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
Copy link

cassyay commented Jan 10, 2020

That worked, thanks!

@TemitopeOladokun
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants