pythonkerasimagedata

Import multiple Images


I want to duplicate my pictures with this code:

from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img

datagen = ImageDataGenerator(
        rotation_range=40,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True,
        fill_mode='nearest')

img = load_img('data/class1/11408_3.jpg')
x = img_to_array(img)  
x = x.reshape((1,) + x.shape)

i = 0
for batch in datagen.flow(x, batch_size=1,
                          save_to_dir='preview', save_prefix='class1', save_format='jpeg'):
    i += 1
    if i > 20:
        break 

With one picture everything works fine. But I have a lot of pictures on this path. How can I get all the pictures - one by one?


Solution

  • Thanks for your Input. I have now solved it with:

    from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
    import glob, os
    
    datagen = ImageDataGenerator(
            rotation_range=90,
            horizontal_flip=True,
            fill_mode='nearest')
    
    os.chdir("dir")
    for file in glob.glob("*.png"):
        img = load_img(file)
        x = img_to_array(img)
        x = x.reshape((1,) + x.shape)
    
        i = 0
        for batch in datagen.flow(x, batch_size=1,save_to_dir='output', save_prefix='new', save_format='jpg'):
            i += 1
            if i > 20:
                break