PythonMagick can’t find my pdf files

I had exactly the same problem couple of days ago. While converting from .gif (oder something else) to .jpg worked really fine, converting from .pdf to .jpg produced exactly the same error. Thats happing because ImageMagick uses Ghostscript for reading/converting PDFs.

You can solve the problem by installing Ghostscript (only 32-bit version works). Don’t forget to add “C:\Program Files (x86)\gs\gs9.06\bin” to your systempath.

Here a step-by-step-guide how I was getting PythonMagick work:
(I’m using Python 2.7.3 32-bit on Windows 7 64-bit.)

  1. Install the newest version of ImageMagick (“ImageMagick-6.8.1-1-Q16-windows-dll.exe” at the moment of writing. Note that this is the 32-bit version; 64-bit works for me fine too).
    DON’T forget to check the option “Install development headers and libraries for C and C++”.
  2. Set “MAGICK_HOME” environment to the path of ImageMagick (for me C:\Program Files (x86)\ImageMagick-6.8.1-Q16).
    Additional set this path to your systemwide-path at the very first position if it isn’t already there.
  3. Download and install the 32-bit version of GhostScript (64 bit won’t work, even if you have installed the 64-bit version of ImageMagick).
    Set C:\Program Files (x86)\gs\gs9.06\bin to your systemwide-path, right after ImageMagick.
  4. Check if your setup works. Try convert some.pdf some.jpg in the command line. If it doesn’t work you’ve done something wrong at point 1-3.
  5. Install PythonMagick with the unofficial binary, not with easy_install or pip.
    (Again: I’m using the 32-bit Python 2.7.3 interpreter, so I took “PythonMagick-0.9.7.win32-py2.7.‌exe” for that.)
  6. Start you Python command line util and try something like this:
from PythonMagick import Image
im = Image()
im.read(r"C:\Path\To\Some.pdf")
im.write("some.jpg")

Additional an example for a PDF with multiple pages:

import os
from pyPdf import PdfFileReader, PdfFileWriter
from tempfile import NamedTemporaryFile
from PythonMagick import Image

reader = PdfFileReader(open("some.pdf", "rb"))
for page_num in xrange(reader.getNumPages()):
    writer = PdfFileWriter()
    writer.addPage(reader.getPage(page_num))
    temp = NamedTemporaryFile(prefix=str(page_num), suffix=".pdf", delete=False)
    writer.write(temp)
    temp.close()

    im = Image()
    im.density("300") # DPI, for better quality
    im.read(temp.name)
    im.write("some_%d.jpg" % (page_num))

    os.remove(temp.name)

That’s the only workaround for that problem which comes into my mind.

Leave a Comment