Skip to content

Instantly share code, notes, and snippets.

@leonardreidy
Created September 19, 2016 11:53
Show Gist options
  • Save leonardreidy/2dcca95a7c14b485dcee06792c6f14e9 to your computer and use it in GitHub Desktop.
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
# 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)
@andrelfnovaes
Copy link

Thanks!

@tucan9389
Copy link

Thanks!

@denysthegitmenace
Copy link

Thank you!

@a-litsov
Copy link

thx, very useful and simple

@sazidabintaislam
Copy link

I am trying to implement the code in windows, where I will include my data path in the code?

@DarthMikeSundays
Copy link

Thanks bro! I was just looking for this! Why? I had an assignement and i needed a workbook from the past year. I hadnt it so a friend sent me photos of each page, but turned 90º degrees to the right!

@paulomo
Copy link

paulomo commented Dec 13, 2020

How do do this while saving the image with different name so you have double the image but with different orientation. Tried to do but just not getting the part with saving the image as a different image.

@Shashika1992
Copy link

How do do this while saving the image with different name so you have double the image but with different orientation. Tried to do but just not getting the part with saving the image as a different image.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment