Skip to content

Instantly share code, notes, and snippets.

@yig
Created August 31, 2015 20:11
Show Gist options
  • Save yig/9f9a5ef888573954182c to your computer and use it in GitHub Desktop.
Save yig/9f9a5ef888573954182c to your computer and use it in GitHub Desktop.
Simple command line script to convert a file from any image format to another. Based on Python Imaging Library. Not based on ImageMagick.
#!/usr/bin/env python2.7
'''
Author: Yotam Gingold <yotam (strudel) yotamgingold.com>
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
'''
import os, sys
def usage():
print >> sys.stderr, 'Usage: %s [--clobber] input output (image type is inferred from the extension)' % (sys.argv[0],)
sys.exit(-1)
argv = list( sys.argv )
del argv[0]
## Optional arguments
kClobber = False
try:
index = argv.index( '--clobber' )
del argv[ index ]
kClobber = True
except ValueError: pass
if len( argv ) != 2:
usage()
inpath = argv[0]
del argv[0]
outpath = argv[0]
del argv[0]
if os.path.exists( outpath ) and not kClobber:
print >> sys.stderr, 'Clobber not specified (--clobber) and outpath exists:', outpath
usage()
import Image
img = Image.open( inpath )
## Quality parameter only affects JPEG's; PIL's default is too low.
img.save( outpath, quality = 90 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment