Skip to content

Instantly share code, notes, and snippets.

@cmg-dev
Created March 17, 2016 10:23
Show Gist options
  • Save cmg-dev/87ac3b3732d63d642e7f to your computer and use it in GitHub Desktop.
Save cmg-dev/87ac3b3732d63d642e7f to your computer and use it in GitHub Desktop.
This little script will help us to get the images straight for our website.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import os, sys
from PIL import Image
size = (768,512)
def compress(files,
output_format="JPEG",
jpg_quality=100,
out_file_name_postfix=".small"):
"""Compress a list of images to smaller versions.
"""
for infile in files:
f, e = os.path.splitext(infile)
outfile = f + out_file_name_postfix + ".jpg"
print("processing '{}' to '{}'".format(infile, outfile))
if infile != outfile:
try:
im = Image.open(infile)
im.thumbnail(size)
im.save(outfile, output_format, quality=jpg_quality)
except IOError:
print("unable to convert '{}'".format(infile))
if __name__ == "__main__":
compress(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment