Skip to content

Instantly share code, notes, and snippets.

@miladfa7
Last active September 11, 2024 14:33
Show Gist options
  • Save miladfa7/d2d89d52deea8abd5e62e9f5b32b56a9 to your computer and use it in GitHub Desktop.
Save miladfa7/d2d89d52deea8abd5e62e9f5b32b56a9 to your computer and use it in GitHub Desktop.
Image Augmentation Python Code
from PIL import Image
from PIL import ImageOps
import os
import random
# set the path to the directory containing the images
input_path = "/path/.../input"
output_path = "/path/.../output"
# loop through each image file in the directory
files = os.listdir(path)
for filename in files:
if filename.endswith(".jpg") or filename.endswith(".png"):
# open the image file
img = Image.open(os.path.join(path, filename))
# apply random rotation
img = img.rotate(15, expand=True)
# apply random horizontal flip
if random.random() > 0.5:
img = ImageOps.mirror(img)
# save the augmented image
img.save(os.path.join(path, "augmented_" + filename))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment