Last active
December 11, 2015 18:19
-
-
Save ifduyue/4640403 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python | |
''' | |
image_convert.py | |
~~~~~~~~~~~~~~~~~ | |
A small tool to resize/compress/rotate image. | |
:copyright: Copyright (C) 2012 Yue Du <[email protected]> | |
:license: BSD 2-Clause License | |
''' | |
import sys | |
import os | |
import optparse | |
from math import ceil | |
import Image, ExifTags | |
parser = optparse.OptionParser('python {0} [OPTION]... INPUTFILE [OUTPUTFILE]' .format(sys.argv[0])) | |
parser.add_option('--maxwidth', dest='maxwidth', default=None, help='max width') | |
parser.add_option('--maxheight', dest='maxheight', default=None, help='max height') | |
parser.add_option('--quality', dest='quality', default=None, help='quality') | |
parser.add_option('--rotate', dest='rotate', default=0, help='rotate') | |
parser.add_option('--format', dest='format', default=None, help='format') | |
options, args = parser.parse_args() | |
if len(args) == 0: | |
parser.print_help() | |
parser.exit(-1, '\nerror: No files\n') | |
inputfile = args[0] | |
outputfile = args[1] if len(args) >= 2 else inputfile | |
options.quality = int(options.quality) if options.quality else 100 | |
im = Image.open(inputfile) | |
if inputfile == outputfile: | |
options.format = options.format or im.format | |
else: | |
ext = outputfile.rsplit('.')[-1].upper() | |
if ext == 'JPG': | |
ext = 'JPEG' | |
if ext not in Image.SAVE: | |
ext = im.format | |
options.format = options.format or ext | |
# auto rotate/flip images which have Orientation. | |
if hasattr(im, '_getexif') and im._getexif(): | |
tags = ExifTags.TAGS | |
exif = dict((tags[k], v) for k, v in im._getexif().items() if k in tags) | |
orientation = exif.get('Orientation', 1) | |
if orientation == 1: | |
pass | |
elif orientation == 2: | |
im = im.transpose(Image.FLIP_LEFT_RIGHT) | |
elif orientation == 3: | |
im = im.rotate(180, Image.BICUBIC, expand=True) | |
elif orientation == 4: | |
im = im.rotate(180, Image.BICUBIC, expand=True) | |
im = im.transpose(Image.FLIP_LEFT_RIGHT) | |
elif orientation == 5: | |
im = im.rotate(270, Image.BICUBIC, expand=True) | |
im = im.transpose(Image.FLIP_LEFT_RIGHT) | |
elif orientation == 6: | |
im = im.rotate(270, Image.BICUBIC, expand=True) | |
elif orientation == 7: | |
im = im.rotate(90, Image.BICUBIC, expand=True) | |
im = im.transpose(Image.FLIP_LEFT_RIGHT) | |
elif orientation == 8: | |
im = im.rotate(90, Image.BICUBIC, expand=True) | |
if im.mode != 'RGBA': | |
im = im.convert('RGBA') | |
if options.rotate: | |
rotate = int(options.rotate) | |
im = im.rotate(rotate, Image.BICUBIC, expand=True) | |
if options.maxwidth: | |
maxwidth = int(options.maxwidth) | |
if im.size[0] > maxwidth: | |
ratio = maxwidth * 1.0 / im.size[0] | |
size = map(lambda x: int(x*ratio), im.size) | |
im = im.resize(size, Image.ANTIALIAS) | |
if options.maxheight: | |
maxheight = int(options.maxheight) | |
if im.size[1] > maxheight: | |
ratio = maxheight * 1.0 / im.size[1] | |
size = map(lambda x: int(x*ratio), im.size) | |
im = im.resize(size, Image.ANTIALIAS) | |
with open(outputfile, 'wb') as f: | |
im.save(f, options.format, quality=options.quality) |
确实会丢EXIF, 有方法可以保持EXIF但是要装第三方库....
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PIL 又慢又会丢 EXIF……