import matplotlib.pyplot as plt
from PIL import Image
%matplotlib inline
from keras.preprocessing import imageimport os
import glob
import numpy as np
img_path='D:/暑假/数据加强/image/sky/*'
in_path='D:/暑假/数据加强/image/'
out_path='D:/暑假/数据加强/out/'
补充说明:
/*:代表文件中所有内容(有个问题需要注意:如果还要在in_path的路径后面加新的路径时,一定要记得加’/‘。
datagen=image.ImageDataGenerator()
gen_data=datagen.flow_from_directory(in_path,batch_size=1,shuffle=False,
save_to_dir=out_path+'resize',
save_prefix='gen',target_size=(224,224))
解释说明:datagen.flow_from_directory()里面的参数很好理解滴,我就说几个:首先由于image文件下就一个文件夹所以呢这个路径用的in_path,save_to_dir:是将变换完的图像放在out文件里的resize文件。save_prefix是图片的前缀呀。其实这个时候文件夹还是空滴!接下来就一切都在不言中了!!!!
4.调用函数显示图片代码:
for i in range(3):
gen_data.next()
print_result(out_path+'resize/*')
结果如下:
datagen=image.ImageDataGenerator(rotation_range=45)#旋转45°
gen=image.ImageDataGenerator()
data=gen.flow_from_directory(in_path,batch_size=1,class_mode=None,shuffle=True,target_size=(224,224))
np_data=np.concatenate([data.next() for i in range(data.n)])
datagen.fit(np_data)
gen_data=datagen.flow_from_directory(in_path,batch_size=1,shuffle=False,save_to_dir=out_path+'rotation',save_prefix="wo",target_size=(224,224))
for i in range(3):
gen_data.next()
print_result(out_path+'rotation/*')
结果如下:
datagen=image.ImageDataGenerator(width_shift_range=0.3,height_shift_range=0.3)
gen_data=datagen.flow_from_directory(in_path,batch_size=1,shuffle=False,save_to_dir=out_path+'shift',save_prefix="w",target_size=(224,224))
for i in range(3):
gen_data.next()
print_result(out_path+'shift/*')
结果如下:
也可是负的参数哦!!
datagen=image.ImageDataGenerator(width_shift_range=-0.3,height_shift_range=0.3)
gen_data=datagen.flow_from_directory(in_path,batch_size=1,shuffle=False,save_to_dir=out_path+'shift2',save_prefix="w",target_size=(224,224))
for i in range(3):
gen_data.next()
print_result(out_path+'shift2/*')
结果如下:
#缩放
datagen=image.ImageDataGenerator(zoom_range=0.5)
gen_data=datagen.flow_from_directory(in_path,batch_size=1,shuffle=False,save_to_dir=out_path+'zoom',save_prefix="gen",target_size=(224,224))
for i in range(3):
gen_data.next()
print_result(out_path+'zoom/*')
结果如下:
#翻转
datagen=image.ImageDataGenerator(horizontal_flip=True)
gen_data=datagen.flow_from_directory(in_path,batch_size=1,shuffle=False,save_to_dir=out_path+'horizontal',save_prefix="w",target_size=(224,224))
for i in range(3):
gen_data.next()
print_result(out_path+'horizontal/*')
Found 3 im
结果如下:
'constant':kkkkkk|abcd|kkkkkk(cval=k)
'nearrst':aaaaaaa|abcd|ddddddd
'reflect':abcddcba|abcd|dcbaabcd
'wrap':abcdabcd|abcd|abcdabcd
1.wrap填充
datagen=image.ImageDataGenerator(fill_mode='wrap',zoom_range=[4,4])
gen_data=datagen.flow_from_directory(in_path,batch_size=1,shuffle=False,save_to_dir=out_path+'fill_mode',save_prefix="w",target_size=(224,224))
for i in range(3):
gen_data.next()
print_result(out_path+'fill_mode/*')
结果如下:
2.reflect填充
datagen=image.ImageDataGenerator(fill_mode='reflect',zoom_range=[4,4])
gen_data=datagen.flow_from_directory(in_path,batch_size=1,shuffle=False,save_to_dir=out_path+'reflect',save_prefix="w",target_size=(224,224))
for i in range(3):
gen_data.next()
print_result(out_path+'reflect/*')
结果如下:
记录每一朵云,因为每一朵云都是独一无二的O(∩_∩)O