Lxs29
Lxs29

Reputation: 175

Python Wand PDF to List of Images TypeError

I'm trying to make a python program that takes a PDF and converts it to a list of images, but I'm getting the issue: 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)

Terminal Output

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

Upvotes: 3

Views: 2085

Answers (1)

ofirdi
ofirdi

Reputation: 481

I had the same issue, here is a step-by-step guide on how I solved it, installing Wand (0.4) and ImageMagick v6 on Mac (macOS High Sierra v 10.13.5)

I'm assuming that ImageMagic is already installed, if not please install it:

brew install imagemagick@6

Next, create a symbolic link, with the following command (replace with your specific version):

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

In my case, it was:

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

Install Wand

pip3 install Wand

Now, when trying to run the code, I got a similar error message:

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’ ‘-

It seems that ghostscript is not installed by default, so let’s install it:

brew install ghostscript

Now you will need to create a soft link to /usr/bin, but /usr/bin/ in OS X 10.11+ is protected.

Just follow these steps:

  1. Reboot to Recovery Mode. Reboot and hold “Cmd + R” after start sound.
  2. In Recovery Mode go to Utilities -> Terminal.
  3. Run: csrutil disable
  4. Reboot in Normal Mode.
  5. Do the “sudo ln -s /usr/local/bin/gs /usr/bin/gs” in terminal.
  6. Do the 1 and 2 step. In terminal enable back csrutil by run: csrutil enable (these steps are based on this)

It worked for me, enjoy!

You can find the my original post here

Upvotes: 1

Related Questions