python wand gives TypeError when trying to create Image from pdf file

2.6k views Asked by At

I'm trying to convert pdf to image files using ImageMagick with wand as a binding. I got stuck with the very first step to create Image object from pdf file.

For this,

from wand.image import Image
with Image(filename="untitled.pdf[0]") as img:
    # do something

I got this error

  File "thumbnailer.py", line 2, in <module>
    with Image(file="untitled.pdf[0]") as img:
  File "C:\Python27\lib\site-packages\wand\image.py", line 2740, in __init__
    self.read(file=file, resolution=resolution)
  File "C:\Python27\lib\site-packages\wand\image.py", line 2805, in read
    raise TypeError('file must be a readable file object'
TypeError: file must be a readable file object, but the given object does not have read() method
Exception TypeError: TypeError("object of type 'NoneType' has no len()",) in <bound method Image.__del__ of <wand.image.Image: (empty)>> ignored

And when I take out array index,

from wand.image import Image
with Image(filename="untitled.pdf") as imgs:
    # do something

I got this error

C:\Dev\serverless\functions-pdf-thumbnail>python thumbnailer.py
Traceback (most recent call last):
  File "thumbnailer.py", line 2, in <module>
    with Image(filename="untitled.pdf") as imgs:
  File "C:\Python27\lib\site-packages\wand\image.py", line 2744, in __init__
    self.read(filename=filename, resolution=resolution)
  File "C:\Python27\lib\site-packages\wand\image.py", line 2820, in read
    r = library.MagickReadImage(self.wand, filename)
WindowsError: exception: stack overflow
Exception TypeError: TypeError("object of type 'NoneType' has no len()",) in <bound method Image.__del__ of <wand.image.Image: (empty)>> ignored

I can't seem to find any document or proper example that create Image from pdf, but some examples on the Internet did it this way. Do you have any idea what went wrong, or what the correct way to do this is?

1

There are 1 answers

1
Matija Munjaković On

Your last block where you show us the error - the error text says you're still attempting to do an index on it

with Image(filename="untitled.pdf[0]") as img: