0

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?

ndrwnaguib
  • 5,623
  • 3
  • 28
  • 51
Timothy_Goodman
  • 393
  • 1
  • 5
  • 18
  • 1
    Loop through all files. [Answer](https://stackoverflow.com/questions/3964681/find-all-files-in-a-directory-with-extension-txt-in-python) – Sebastian Waldbauer Jan 28 '19 at 12:36
  • 1
    Possible duplicate of [Find all files in a directory with extension .txt in Python](https://stackoverflow.com/questions/3964681/find-all-files-in-a-directory-with-extension-txt-in-python) – ndrwnaguib Jan 28 '19 at 13:04

2 Answers2

0

You could use a string list which contains every single file path and put that in a for loop like this

filePaths = ['data/class1/11408_3.jpg', ...]
for file in filePaths:
    img = load_img(file)
tphiR
  • 1
0

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 
Timothy_Goodman
  • 393
  • 1
  • 5
  • 18