Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tensorflow.keras.preprocessing.image import ImageDataGenerator
- import matplotlib.pyplot as plt
- import numpy as np
- import os
- from tensorflow.keras.preprocessing.image import load_img, img_to_array, array_to_img
- # Load the single image
- img_path = 'path_to_your_image.jpg'
- img = load_img(img_path)
- x = img_to_array(img)
- x = x.reshape((1,) + x.shape)
- # Define the augmentation configuration
- 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')
- # Generate batches of augmented images and save them
- save_to_dir = 'path_to_save_augmented_images'
- if not os.path.exists(save_to_dir):
- os.makedirs(save_to_dir)
- i = 0
- for batch in datagen.flow(x, batch_size=1, save_to_dir=save_to_dir, save_prefix='aug', save_format='jpeg'):
- i += 1
- if i > 20:
- break # Generate 20 augmented images
- # Visualize the augmented images
- for file in os.listdir(save_to_dir):
- img = load_img(os.path.join(save_to_dir, file))
- plt.imshow(img)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement