Last active
September 11, 2024 14:33
-
-
Save miladfa7/d2d89d52deea8abd5e62e9f5b32b56a9 to your computer and use it in GitHub Desktop.
Image Augmentation Python Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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