Convert all PDF files in a directory to PNG images

I just needed to convert several PDF vector graphics to PNG graphics. A clean way to do this is via Ghostscript (example: gs -sDEVICE=png16m -sOutputFile=tiger.png tiger.pdf). For convenience, I made a Python script that converts PDF files to PNG files via Ghostscript. I use it under Linux and Windows.

Update (December 2012)
: It just appeared to me that I have a more consolidated version of the script in my code repository at bitbucket. I have updated the code below with the current version.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#   Copyright (C) 2009-2012 Jan-Philip Gehrcke
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
import subprocess
import os
import traceback
import sys
# Absolute path to Ghostscript executable here or command name if Ghostscript is
# in your PATH.
def usage_exit():
        sys.exit("Usage: %s png_resolution pdffile1 pdffile2 ..." %
def main():
    if not len(sys.argv) >= 3:
        resolution = int(sys.argv[1])
    except ValueError:
    for filepath in sys.argv[1:]:
        (name, ext) = os.path.splitext(filepath)
        if ext.lower().endswith("pdf"):
            print "*** Converting %s..." % filepath
            gs_pdf_to_png(os.path.join(os.getcwd(), filepath), resolution)
def gs_pdf_to_png(pdffilepath, resolution):
    if not os.path.isfile(pdffilepath):
        print "'%s' is not a file. Skip." % pdffilepath
    pdffiledir = os.path.dirname(pdffilepath)
    pdffilename = os.path.basename(pdffilepath)
    pdfname, ext = os.path.splitext(pdffilepath)
        # Change the "-rXXX" option to set the PNG's resolution.
        # For other commandline options see
        arglist = [GHOSTSCRIPTCMD,
                  "-sOutputFile=%s.png" % pdfname,
                  "-r%s" % resolution,
        print "Running command:\n%s" % ' '.join(arglist)
        sp = subprocess.Popen(
    except OSError:
        sys.exit("Error executing Ghostscript ('%s'). Is it in your PATH?" %
        print "Error while running Ghostscript subprocess. Traceback:"
        print "Traceback:\n%s"%traceback.format_exc()
    stdout, stderr = sp.communicate()
    print "Ghostscript stdout:\n'%s'" % stdout
    if stderr:
        print "Ghostscript stderr:\n'%s'" % stderr
if __name__ == "__main__":