Created
March 30, 2017 13:23
-
-
Save d0zingcat/079e3089715a6c58a2293b873cda4a9d to your computer and use it in GitHub Desktop.
This is a py-program to patch JPEG Photos to prevent auto-rotate in markdown files.
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 | |
import os, argparse, pexif | |
def picProcessing(picPath, isForce=False): | |
try: | |
img = pexif.JpegFile.fromFile(picPath) | |
#Get the orientation if it exists | |
orientation = img.exif.primary.Orientation[0] | |
print "The orientation of this pic is", orientation, "(will change to 1)" | |
# Set Orientation to 1 | |
img.exif.primary.Orientation[0] = 1 | |
img.writeFile(picPath) | |
# now rotate the image using the Python Image Library (PIL) | |
print "Preparing to rotate the image..." | |
img = Image.open(picPath) | |
exifInfo = img.info['exif'] | |
if isForce: | |
img = img.rotate(-90) | |
elif orientation is 6: img = img.rotate(-90, expand=1) | |
elif orientation is 8: img = img.rotate(90, expand=1) | |
elif orientation is 3: img = img.rotate(180, expand=1) | |
elif orientation is 2: img = img.transpose(Image.FLIP_LEFT_RIGHT) | |
elif orientation is 5: img = img.rotate(-90, expand=1).transpose(Image.FLIP_LEFT_RIGHT) | |
elif orientation is 7: img = img.rotate(90, expand=1).transpose(Image.FLIP_LEFT_RIGHT) | |
elif orientation is 4: img = img.rotate(180, expand=1).transpose(Image.FLIP_LEFT_RIGHT) | |
#save the result | |
img.save(picPath, exif=exifInfo) | |
except: | |
print "ERROR!!!" | |
pass | |
def args_settings(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-d", "--directory", | |
help="Desired directory to be processed.", default=os.getcwd()) | |
parser.add_argument("-A", "--all", help="Processing all the files under this directory", | |
action="store_true", default=False) | |
parser.add_argument("-f", "--filename", | |
help="Desired filename to be processed(extension is needed).", | |
default='') | |
parser.add_argument("-F", "--force", action="store_true", | |
help="Ignore the Orientation Info and rotate the image file of 90 degrees.") | |
parser.add_argument("-r", "--recursive", action="store_true", | |
help="Recursively visit the files under the subfolders.") | |
args = parser.parse_args() | |
return args | |
def fileChooser(args): | |
isAll = args.all | |
filename = args.filename | |
directory = args.directory | |
isRecursive = args.recursive | |
if args.force: | |
isForce = True | |
else: | |
isForce = False | |
if os.path.exists(directory): | |
if isAll: | |
if isRecursive: | |
print "You've choosed the all the files under this directory." | |
for root, subdirs, files in os.walk(directory): | |
for File in files: | |
if File.lower().endswith(('.jpg', '.jpeg')): | |
picPath = os.path.join(directory, root, File) | |
if (os.path.isfile(picPath)): | |
print "Processing...", File | |
picProcessing(picPath, isForce) | |
else: | |
print "File", File, "does not exist!" | |
else: | |
print "You've choosed the files under current directory." | |
for thisFile in os.listdir(directory): | |
if thisFile.lower().endswith(('.jpg', '.jpeg')): | |
picPath = os.path.join(directory, thisFile) | |
if os.path.isfile(picPath): | |
print "Processing...", thisFile | |
picProcessing(picPath, isForce) | |
else: | |
print "File", thisFile, "does not exist!" | |
else: | |
if args.filename == '': | |
print "You must specify the filename if you choose not all files." | |
else: | |
print "You've choosed '{}' to process.".format(filename) | |
filepath = os.path.join(directory, filename) | |
if os.path.isfile(filepath): | |
print "Processing...", filename | |
picProcessing(filepath, isForce) | |
print "Done!" | |
else: | |
print "File does not exist!" | |
else: | |
print "The directory does not exist!" | |
if __name__ == "__main__": | |
args = args_settings() | |
fileChooser(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment