All pastes #164198 Raw Edit

Untitled

public text v1 · immutable
·published 2006-09-08 11:43 UTC
rendered paste body
#!/usr/bin/env python

"""Changes the format of an image.
"""

__author__ = 'Aris Mikropoulos <indy90@gmail.com>'
__date__ = '8-9-2006'
__version__ = '1.0'
__license__ = 'GPL'
__copyright__ = 'Copyright (c) Aris Mikropoulos, 2006-2007'

import sys , os , Image, getopt, os.path

def usage (progname):
  sys.stdout.write( 'usage: %s -f format filename\n' % progname)

def convertimage (filename, newformat):
  name, ext = os.path.splitext (filename)
  newfilename = "%s.%s" % (name, newformat)
  try:
    Image.open (filename).save (newfilename)
    os.unlink( filename )
  except IOError:
    sys.stderr.write( 'cannot open %s\n' % filename )
  except KeyError :
    sys.stderr.write( 'unknown image format: %s\n' % newformat)

options, files = getopt.getopt (sys.argv[1:], 'f:') 
format = ''
for opt, value in options:
  if opt == '-f': format = value

if not (files or format):
  usage (sys.argv[0])
else:
  for filename in files:
    convertimage (filename, format)