本文介绍了PythonMagick找不到我的pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从非官方的Windows二进制文件.

我正在尝试运行此代码(Processor.py)

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')

在此文件夹中(D:\ Python Projects \ Sheet Music Reader)

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

但是,使用该相对pdf路径或pdf = "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

我根本不明白为什么找不到我的pdf文件;它与python脚本位于同一目录中.

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

什么原因导致此错误,以及如何解决?
(我给人的印象是,将pdf转换为python中的图像是一场噩梦)

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)

推荐答案

几天前,我遇到了完全相同的问题.从.gif(其他东西)转换为.jpg确实很好,但从.pdf转换为.jpg产生了完全相同的错误.之所以如此,是因为ImageMagick使用Ghostscript来读取/转换PDF.

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.

您可以通过安装 Ghostscript 解决问题(仅32位版本有效) .不要忘记在系统路径中添加"C:\ Program Files(x86)\ gs \ gs9.06 \ bin".

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.

这里有一个逐步指南,介绍如何使PythonMagick工作:
(我在Windows 7 64位系统上使用32位Python 2.7.3.)

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. 安装最新版本的 ImageMagick ("ImageMagick-6.8.1-1-Q16- Windows-dll.exe".请注意,这是32位版本; 64位也对我有效).
    不要忘了选中选项为C和C ++安装开发头文件和库"..
  2. 将" MAGICK_HOME "环境设置为ImageMagick的路径(对我来说是C:\Program Files (x86)\ImageMagick-6.8.1-Q16).
    如果还没有,请另外将此路径设置为您的systemwide-path的第一位置.
  3. 下载并安装32位版本的 GhostScript (64位不会即使您已经安装了ImageMagick的64位版本也可以正常工作.
    在ImageMagick之后,将C:\Program Files (x86)\gs\gs9.06\bin设置为系统范围的路径.
  4. 检查您的设置是否有效.在命令行中尝试convert some.pdf some.jpg.如果它不起作用,则说明您在1-3点做错了事.
  5. 使用非官方二进制文件而不是easy_install或pip安装 PythonMagick .
    (再次:我使用的是32位Python 2.7.3解释器,因此我选择了"PythonMagick-0.9.7.win32-py2.7.‌exe".)
  6. 启动您的Python命令行实用程序,然后尝试执行以下操作:
  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")



多页PDF的其他示例:



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.

这篇关于PythonMagick找不到我的pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:29