eps2png

This script uses ghostsript to convert eps figures into png.

#! /usr/bin/env python

import os,sys,optparse

# parse the command line with optparse
usestr = '%prog [-g WIDTHxHEIGHT] INPUTFILE OUTPUTFILE'
parser = optparse.OptionParser(usage=usestr)
parser.add_option('-g','--geometry',action="store",dest='geometry',
                  help='specify geometry [default from bounding box]')
parser.add_option('--debug',action="store_true",help='verbose output')
arg = parser.parse_args()
opt = arg[0]

try:
    inputfile = str(arg[1][0])
    outputfile = str(arg[1][1])
except (IndexError,ValueError):
    parser.error('give INPUTFILE and OUTPUTFILE')

if opt.geometry:
    try:
        split = opt.geometry.strip().split('x')
        width = int(split[0])
        height = int(split[1])
    except (IndexError,AttributeError):
        parser.error('geometry must be in the format WIDTHxHEIGHT')
else:
    # open epsfile and look for bounding box
    f = open(inputfile,'r')
    for line in f:
        if line.startswith('%%BoundingBox'):
            split = line.split()
            try:
                width = int(split[3])-int(split[1])
                height = int(split[4])-int(split[2])
            except IndexError:
                raise RuntimeError('could not find bounding box in eps file')
            break
    f.close()

# invoking gs
call = 'gs -dNOPAUSE -sDEVICE=png16m -q -dBATCH -dEPSFitPage -sOutputFile=%s ' \
    '-g%ix%i %s' % (outputfile,width,height,inputfile)
if opt.debug: print call
    os.system(call)
1