Instead of writing your own function to do this, depend on the built-in functions a higher-level machine learning/deep learning module provides.
Like in the Keras module there is a built-in function called ImageDataGenerator()
This function has two arguments for generating shifts in the image. One for horizontal shift and the other for vertical shift. These two arguments are:
width_shift_range,
height_shift_range
Each of these arguments take: a Float, a 1-D array-like or an int.
1). float: fraction of total height, if < 1, or pixels if >= 1.
2). 1-D array-like: random elements from the array.
3). int: integer number of pixels from interval (-height_shift_range, +height_shift_range)
Now, coming to the fact that you want to augment these images and save them all in the same folder, use this piece of code:
aug = ImageDataGenerator(width_shift_range=0.2,height_shift_range=0.2)
### Make sure to have "/" at the end of the path
list_of_images=os.listdir("/path/to/the/folder/of/images/")
total = 0
#Change the value of "const" to the number of new augmented images to be created
const= 300
for i in range(const):
curr_image = random.choice(list_of_images)
image = load_img("/path/to/the/folder/of/images/"+curr_image)
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
imageGen = aug.flow(image, batch_size=1, save_to_dir='/path/to/folder/to/save/images/',save_prefix="augment_image",save_format="jpg")
for image in imageGen:
total += 1
if total == const:
break
break
This above snippet will create 300 new images in a folder called: "/path/to/folder/to/save/images/". Then all you have to do is paste your original images into this folder.
There are other arguments you can give for ImageDataGenerator() like brightness, zoom, vertical flip, horizontal flip, etc. Look into the documentation for more such arguments.