0

我想用这段代码复制我的照片:

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 

一张照片一切正常。但是我在这条路上有很多照片。我怎样才能得到所有的照片 - 一张一张?

4

2 回答 2

0

您可以使用包含每个文件路径的字符串列表,并将其放在这样的 for 循环中

filePaths = ['data/class1/11408_3.jpg', ...]
for file in filePaths:
    img = load_img(file)
于 2019-01-28T12:39:11.363 回答
0

感谢您的输入。我现在已经解决了:

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 
于 2019-01-30T14:56:32.590 回答