Created
September 19, 2016 11:53
-
-
Save leonardreidy/2dcca95a7c14b485dcee06792c6f14e9 to your computer and use it in GitHub Desktop.
Simple function to rotate all images in the current directory with Python Pillow
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
# pip install Pillow if you don't already have it | |
# import image utilities | |
from PIL import Image | |
# import os utilities | |
import os | |
# define a function that rotates images in the current directory | |
# given the rotation in degrees as a parameter | |
def rotateImages(rotationAmt): | |
# for each image in the current directory | |
for image in os.listdir(os.getcwd()): | |
# open the image | |
img = Image.open(image) | |
# rotate and save the image with the same filename | |
img.rotate(rotationAmt).save(image) | |
# close the image | |
img.close() | |
# examples of use | |
rotateImages(90) | |
rotateImages(180) | |
rotateImages(270) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
img = Image.open(image_path)
temp_rot_image = img.rotate(rotation_amt)
new_image_folder= 'data\Kaggle\aug_images\'+str(temp_idx) # edit here with the preferred image folder
if not os.path.exists(new_image_folder): # additional, creating folder if not existed
os.makedirs(new_image_folder)
temp_rot_image.save(os.path.join(new_image_folder, new_image_name)) # save roatetd image