Created
January 11, 2012 16:13
-
-
Save messa/1595397 to your computer and use it in GitHub Desktop.
imgresize
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
#!/usr/bin/env python | |
import optparse | |
from PIL import Image | |
def main(): | |
op = optparse.OptionParser() | |
options, args = op.parse_args() | |
filename, size = args | |
width, height = size.split("x") | |
width, height = int(width), int(height) | |
im = Image.open(filename) | |
imWidth, imHeight = im.size | |
print "%s: %s %dx%d" % (filename, im.format, imWidth, imHeight) | |
xScale = float(width) / imWidth | |
yScale = float(height) / imHeight | |
scale = max(xScale, yScale) | |
cropWidth = int(width / scale) | |
cropHeight = int(height / scale) | |
print "crop %dx%d" % (cropWidth, cropHeight) | |
im = im.crop((0, 0, cropWidth, cropHeight)) | |
im = im.resize((width, height)) | |
newFilename = "%s-%dx%d.jpg" % (filename, width, height) | |
print "Saving to %s" % newFilename | |
im.save(newFilename) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment