PythonMagick罐头';找不到我的pdf文件
我已经从 我正在尝试运行此代码(Processor.py) 在此文件夹中(D:\Python项目\Sheet Music Reader) 但是,使用该相对pdf路径或PythonMagick罐头';找不到我的pdf文件,python,windows,pdf,runtime-error,pythonmagick,Python,Windows,Pdf,Runtime Error,Pythonmagick,我已经从 我正在尝试运行此代码(Processor.py) 在此文件夹中(D:\Python项目\Sheet Music Reader) 但是,使用该相对pdf路径或pdf=“D:\\Python Projects\\Sheet Music Reader”会导致此错误 Traceback (most recent call last): File "D:/Python Projects/Sheet Music Reader/Processor.py", line 6, in <mod
pdf=“D:\\Python Projects\\Sheet Music Reader”
会导致此错误
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
回溯(最近一次呼叫最后一次):
文件“D:/Python Projects/Sheet Music Reader/Processor.py”,第6行,在
p、 阅读(pdf)
运行时错误:Magick:PostscriptDelegateFailed`D:\Python项目\Sheet Music Reader\test.pdf':
没有这样的文件或目录@error/pdf.c/ReadPDFImage/664
我只是不明白为什么它找不到我的pdf;它与python脚本位于同一目录中
导致此错误的原因是什么?如何修复它?(我的印象是,用python将PDF转换为图像是一件麻烦事)几天前我遇到了完全相同的问题。虽然从.gif(或其他文件)转换为.jpg工作得非常好,但从.pdf转换为.jpg会产生完全相同的错误。这是因为ImageMagick使用Ghostscript读取/转换PDF 您可以通过安装(仅32位版本有效)来解决此问题。别忘了将“C:\ProgramFiles(x86)\gs\gs9.06\bin”添加到您的系统路径中。
下面是我如何让PythonMagick工作的分步指南:
(我正在Windows 7 64位上使用Python 2.7.3 32位。)
不要忘记选中“为C和C++安装开发头和库”选项
C:\Program Files(x86)\ImageMagick-6.8.1-Q16
)。另外,如果尚未设置此路径,请在第一个位置将其设置为系统范围的路径
将
C:\ProgramFiles(x86)\gs\gs9.06\bin
设置为系统范围的路径,就在ImageMagick之后(同样:我使用的是32位Python 2.7.3解释器,所以我采用了“PythonMagick-0.9.7.win32-py2.7”。exe“用于此。)
从PythonMagick导入图像
im=图像()
im.read(r“C:\Path\To\Some.pdf”)
im.write(“some.jpg”)
具有多个页面的PDF的其他示例:
这是我想到的解决这个问题的唯一方法。os.path.exists(“D:\\Python项目\\ Sheet Music Reader\\test.pdf”?是否需要文件名或文件对象?
os.path.exists()
返回True,将文件对象作为pdf传递会引发错误;ArgumentError:Image.read(Image,file)中的Python参数类型与C++签名不匹配:< /Cord> @反地球:你尝试过了吗?它比PythonMagick有更多的python接口。在安装GHOSTScript并将该字符串添加到我的路径之后,我仍然收到同样的错误。我隐约记得PYPDF有问题,并且记得“Python 2.5”。(我也听说过让魔杖工作的坏处)也许使用64位python是另一个正在进行的工作。Java是否有任何已知的和功能性的pdf API?添加了一个分步指南。添加了一个包含多个页面的pdf示例。很抱歉延迟。@akki您需要设置一个名为MAGICK_HOME的新环境变量,以及ImageMagick安装的路径(在我的示例中)“C:\Program Files(x86)\ImageMagick-6.8.1-Q16”)作为值。
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
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)