0

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?

msrc
  • 663
  • 1
  • 9
  • 19
  • The first error doesn't match the code. Note that `Image.read` method behaves differently if called with `file=`, or `filename=` kwargs. The latter being the correct method in your use case. Now the second error is interesting. It seems to suggest that your OS may not have enough resources, or there's a greater issue elsewhere in the codebase. How large is the `untitled.pdf`? – emcconville Dec 20 '16 at 16:09
  • @emcconville I copied and pasted many error and seems like I made a mistake, but the idea is the same. The file is just a two page pdf, about 100KB. I think there is something wrong with ImageMagick because when I tried directly with command line, it gave me the same error when I tried to convert pdf. It works fine with png and such though... – msrc Dec 21 '16 at 14:21

1 Answers1

0

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: