Skip to content

Instantly share code, notes, and snippets.

@PureMath86
Created March 5, 2018 20:22
Show Gist options
  • Save PureMath86/aa33e45637a70cce61ff7ca22e4d65a1 to your computer and use it in GitHub Desktop.
Save PureMath86/aa33e45637a70cce61ff7ca22e4d65a1 to your computer and use it in GitHub Desktop.
rotate_img.py
# import packages
from PIL import Image
import os
import argparse # build command-line interfaces
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-d",
"--directory",
required=True,
help="path to input directory")
args = vars(ap.parse_args())
# load the directory path
directory = args["directory"]
for filename in os.listdir(directory):
if filename.endswith(".jpg") or filename.endswith(".jpeg"):
image = os.path.join(directory, filename)
# print(image)
# Load the original image:
img = Image.open(image)
# couterclockwise rotation
img2 = img.rotate(90, expand=True)
img2.save(image)
else:
pass
# Resampling filters
# ==================
#
# Nearest Neighbor (default):
# img2 = img.rotate(45, respample=Image.NEAREST)
#
# Linear interpolation:
# img2 = img.rotate(45, respample=Image.BILINEAR)
#
# Cubic-spline interpolation:
# img2 = img.rotate(45, respample=Image.BICUBIC)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment