Skip to content

Instantly share code, notes, and snippets.

@messa
Created January 11, 2012 16:13
Show Gist options
  • Save messa/1595397 to your computer and use it in GitHub Desktop.
Save messa/1595397 to your computer and use it in GitHub Desktop.
imgresize
#!/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