Convert all PDF files in a directory to PNG images

I just needed to convert several PDF graphics to PNG raster graphics. The cleanest 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 all PDF files in the working directory to PNG files via Ghostscript. I use it under Windows, but this works for “all” operating systems. Let me share it with you.

Python script:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import subprocess
import os
import traceback
 
GHOSTSCRIPTPATH = "C:/Program Files (x86)/gs/gs8.70/bin/gswin32c.exe"
 
def main():
    cwd = os.getcwd()
    for filename in os.listdir(cwd):
        (name, ext) = os.path.splitext(filename)
        if ext.lower().endswith("pdf"):
            print "*** FOUND %s" % filename
            convert_pdf_file_to_png(GHOSTSCRIPTPATH,
                os.path.join(cwd, filename))
 
def convert_pdf_file_to_png(ghostscriptpath, pdffilepath):
    if not os.path.isfile(pdffilepath):
        print "%s is not a file" % pdffilepath
        return False
    if not os.path.isfile(ghostscriptpath):
        print "%s is not a file" % ghostscriptpath
        return False
 
    pdffiledir = os.path.dirname(pdffilepath)
    pdffilename = os.path.basename(pdffilepath)
    pdfname, ext = os.path.splitext(pdffilepath)
 
    try:    
        # change the "-rXXX" option to set the PNG's resolution.
        # -> http://ghostscript.com/doc/current/Devices.htm#File_formats
        # for other commandline options see
        # http://ghostscript.com/doc/current/Use.htm#Options
        arglist = [ghostscriptpath,
                  "-dBATCH",
                  "-dNOPAUSE",
                  "-sOutputFile=%s.png" % pdfname,
                  "-sDEVICE=png16m",
                  "-r800",
                  pdffilepath]
 
        print "try running cmd:\n%s" % arglist
 
        sp = subprocess.Popen(
            args=arglist,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
    except:
        print "Error while running Ghostscript subprocess. Traceback:"
        print "Traceback:\n%s"%traceback.format_exc()
        return False
    # wait for process to terminate, get stdout and stderr
    stdout, stderr = sp.communicate()
    print "Ghostscript subprocess STDOUT:\n%s" % stdout
    if stderr:
        print "Ghostscript STDERR:\n%s" % stderr
        return False
    return True
 
 
if __name__ == "__main__":
    main()
Usage:
  • Download & Install Python 2.6.x: http://python.org/download/ (should work with Python 2.4 and above, but not with Python 3)
  • Download & Install Ghostscript: http://mirror.cs.wisc.edu/pub/mirrors/ghost/GPL/current/
  • Copy the script source from above, save it as e.g. make_all_pdfs_to_png.pyw
  • adjust the variable GHOSTSCRIPTPATH in line 5 of the script to the location of your Ghostscript executable. Even for Windows, it’s okay to use normal slashes in the path (or escaped backslashes: \\).
  • If needed, change the resolution option “-rXXX” in line 38 of the script. This determines the resolution of the PNGs (read in the Ghostscript documentation about the exact meaning of the options — links to the docs are given within the source above).
  • Now the Python script is ready to use. Copy it to the directory containing the PDF files you want to convert and start it (under Windows: simply double click the script file). The corresponding PNG files should be created now.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">