【问题标题】:Python Wand PDF to List of Images TypeErrorPython Wand PDF to List of Images TypeError
【发布时间】:2018-01-23 18:17:24
【问题描述】:

我正在尝试制作一个 python 程序,该程序接受 PDF 并将其转换为图像列表,但我遇到了问题:TypeError: object of type 'NoneType' has no len()

Python

from wand.image import Image as Ima

images = []

pdf = Ima(filename="./sample.pdf")
jpeg = pdf.convert('jpeg')

for img in jpeg.sequence:
    img_page = Image(image=img)
    images.append(img_page.make_blob('jpeg'))

print(images)

终端输出

File "OCR.py", line 9, in <module>
pdf = Ima(filename="./sample.pdf")
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/wand/image.py", line 2744, in __init__
self.read(filename=filename, resolution=resolution)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/wand/image.py", line 2822, in read
self.raise_exception()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/wand/resource.py", line 222, in raise_exception
raise e
wand.exceptions.DelegateError: FailedToExecuteCommand `'gs' -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r72x72'  '-sOutputFile=/var/folders/ll/vhx7d69s27l_766f7fh0w8t80000gn/T/magick-17265E7LIPpQmRZ4H%d' '-f/var/folders/ll/vhx7d69s27l_766f7fh0w8t80000gn/T/magick-17265VZNfoxE7irl0' '-f/var/folders/ll/vhx7d69s27l_766f7fh0w8t80000gn/T/magick-17265OAxHJmx1Guv6'' (1) @ error/pdf.c/InvokePDFDelegate/292
Exception ignored in: <bound method Resource.__del__ of <wand.image.Image: (empty)>>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/wand/resource.py", line 232, in __del__
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/wand/image.py", line 2767, in destroy
TypeError: object of type 'NoneType' has no len()

【问题讨论】:

  • 系统上是否安装了ghostscript?
  • 不,不是。现在我收到错误File "OCR.py", line 13, in &lt;module&gt; img_page = Image(image=img) TypeError: 'module' object is not callable
  • ... 因为 Image(image=img) 应该是 Ima(image=img) ?
  • @emcconville 你是对的,应该回答...
  • 问题在于 wand.sequence.SingleImage 没有属性 make_blob。只有 wand.image.Image 有它。

标签: python pdf imagemagick wand


【解决方案1】:

我遇到了同样的问题,这是我如何解决它的分步指南,在 Mac 上安装 Wand (0.4) 和 ImageMagick v6 (macOS High Sierra v 10.13.5)

我假设 ImageMagic 已经安装,如果没有请安装它:

brew install imagemagick@6

接下来,创建一个符号链接,使用以下命令(替换为您的特定版本):

ln -s /usr/local/Cellar/imagemagick@6//lib/libMagickWand-6.Q16.dylib /usr/local/lib/libMagickWand.dylib

就我而言,它是:

ln -s /usr/local/Cellar/imagemagick@6/6.9.10-0/lib/libMagickWand-6.Q16.dylib /usr/local/lib/libMagickWand.dylib

安装魔杖

pip3 install Wand

现在,在尝试运行代码时,我收到了类似的错误消息:

wand.exceptions.DelegateError: FailedToExecuteCommand `'gs' -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 ‘-sDEVICE=pngalpha’ -dTextAlphaBits=4 -dGraphicsAlphaBits=4 ‘-r72x72’ '-

好像ghostscript默认没有安装,我们来安装吧:

brew install ghostscript

现在您需要创建指向 /usr/bin 的软链接,但 OS X 10.11+ 中的 /usr/bin/ 受到保护。

只需按照以下步骤操作:

  1. 重新启动到恢复模式。重启并在启动声音后按住“Cmd + R”。
  2. 在恢复模式下,转到实用程序 -> 终端。
  3. 运行:csrutil disable
  4. 以正常模式重新启动。
  5. 在终端中执行“sudo ln -s /usr/local/bin/gs /usr/bin/gs”。
  6. 执行第 1 步和第 2 步。在终端中通过运行启用 csrutil:csrutil enable (这些步骤基于this

它对我有用,享受吧!

你可以找到我的原帖here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多