11

I've downloaded and installed PythonMagick for python 2.7, 64 bit Windows 7, from the Unofficial Windows Binaries.

I am trying to run this code (Processor.py)

import PythonMagick

pdf = 'test.pdf'
p = PythonMagick.Image()    
p.density('600')
p.read(pdf)
p.write('doc.jpg')

within this folder (D:\Python Projects\Sheet Music Reader) D:\Python Projects\Sheet Music Reader

However, using that relative pdf path or pdf = "D:\\Python Projects\\Sheet Music Reader" results in this error;

Traceback (most recent call last):
  File "D:/Python Projects/Sheet Music Reader/Processor.py", line 6, in <module>
    p.read(pdf)  
RuntimeError: Magick: PostscriptDelegateFailed `D:\Python Projects\Sheet Music Reader\test.pdf':   
No such file or directory @ error/pdf.c/ReadPDFImage/664

I simply don't understand why it can't find my pdf; it's in the same directory as the python script.

What's causing this error, and how do I fix it?
(I've on the impression that converting pdfs to images in python is a night mare)

Anti Earth
  • 4,671
  • 13
  • 52
  • 83
  • What's the result of `os.path.exists("D:\\Python Projects\\Sheet Music Reader\\test.pdf"`? Does `p.read(pdf)` expect a filename or file object? – Alex L Dec 21 '12 at 05:30
  • `os.path.exists()` returns True, and passing a file object as pdf raises the error; `ArgumentError: Python argument types in Image.read(Image, file) did not match C++ signature:` – Anti Earth Dec 21 '12 at 06:31

1 Answers1

27

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.

floqqi
  • 1,137
  • 2
  • 10
  • 19
  • @Anti Earth: Have you tried using [wand](http://pypi.python.org/pypi/Wand)? It has a much more pythonic interface than PythonMagick. – floqqi Dec 21 '12 at 09:31
  • I'm still receiving the same error, after installing Ghostscript and adding that string to my Path. I faintly recall having issues with pyPdf and remember 'python 2.5' was involved. (I've also heard bad bad things about getting wand to work) Perhaps using 64 bit python is another spanner in the works. Does Java have any know-and-functional pdf APIs? – Anti Earth Dec 22 '12 at 05:04
  • Added a step-by-step-guide. – floqqi Dec 22 '12 at 10:34
  • The pyPdf example (I believe) is invalid; you are passing a pyPdf page object (whatever it is) to a PythonMagick Image constructor, which doesn't accept it (causing an argument error; C++ type mismatch). I can't find any valid examples of the two modules cooperating. Do you have any other resources/examples? Thanks – Anti Earth Dec 23 '12 at 08:15
  • 2
    Added an example for a PDF with multiple pages. Sorry for the delay. – floqqi Dec 25 '12 at 13:51
  • Aha, I see. I don't know why that didn't occur to me :| Thanks x 100000000 – Anti Earth Dec 26 '12 at 03:16
  • This related question skips the temporary files and makes it load the relevant pages directly: http://stackoverflow.com/questions/10489960/how-to-handle-multi-page-images-in-pythonmagick/11786907#11786907 – Ivo Flipse Jan 24 '13 at 10:34
  • same problem here, however I'm using python 2.7.3 64 bits with ImageMagick 64 bits. Ghostscript 32 bits didn't work, I had to install the 64 bit version for it to work (using floqqi's step-by-step guide). – Rabih Kodeih Jun 19 '13 at 21:58
  • @floqqi What does 'Set "MAGICK_HOME" environment to the path of ImageMagick' mean; How can I do that ? And the name of the ImageMagick setup file has been changed to ImageMagick-6.9.1-2-Q16-x86-dll.exe (even the version is updated) now. – akki May 12 '15 at 13:04
  • 1
    @akki You need to set a new environment variable named MAGICK_HOME with the path of your ImageMagick installation (in my example "C:\Program Files (x86)\ImageMagick-6.8.1-Q16") as the value. – floqqi May 12 '15 at 17:23