2

I am trying to convert a PDF to a JPG using wand.

I would like to adjust the size of the jpg based on the docHeight, which is a global variable.

The size of the jpg would be determined by the max dimension's ratio with the target page it will be placed on.

I seem to be failing right from the starting gate, and I cannot figure out why.

It seems the file is not being read in my with statement.

my with Image statement returns

>>>>with Image(filename = thumbOriginal, resolution = (RESOLUTION,RESOLUTION)) as img:
    print img
'NoneType' object has no attribute '__getitem__'

What is wrong with how I am reading the file?

it is mostly borrowed from this question. I can barely find any info on how to properly make this conversion.

    with Image(filename = thumbOriginal, resolution = (RESOLUTION,RESOLUTION)) as img:
        print img # prints 'NoneType' object has no attribute '__getitem__'
        img_width = img.width
        print "img_width: "+str(img_width)  # prints 0 if I comment out "print img" statement
        img_height = img.height
        print "img_height: "+str(img_height)  # prints 0 if I comment out "print img" statement
        if img_width > img_height:
            ratio = float(dest_width / img_width) # Fails for float divided by 0 if I comment out "print img" statement
        else: ratio = float(dest_height / img_height) # Fails for float divided by 0 if I comment out "print img" statement

        img.resize(int(ratio*img_width), int(ratio *img.height))
        img.alpha_channel = False
        img.format = 'jpg'
        img.save(filename = thumb)

I've also tried a simpler version I found on a different stackoverflow question:

with Image(filename = thumbOriginal, resolution = 300) as img:
    print img
    img.compression_quality = 99
    img.save(filename=thumb)

but am getting the same error

'NoneType' object has no attribute '__getitem__'

I know that the pdf is there. I can open it manually and it works just fine.

I have also tried removing the resolution argument altogether with no luck.

When I include a ...

print thumbOriginal

.... statement, it returns the correct and complete file path.

Community
  • 1
  • 1
ExperimentsWithCode
  • 1,174
  • 4
  • 14
  • 30

1 Answers1

0

img is not a string, it is an Image object (look at the Wand source code to understand). When you try to print an Image object python has no idea how to do that. When you say:

print thumbObject

you have defined this (ostensibly) as a path to a filename which is a string. The print method knows how to print a string.

I think if you try the below code you'll have better luck. Notice that I specified resolution:

with Image(filename=thumbOriginal, resolution=300) as img:
        img_width = img.width
        print "img_width: "+str(img_width)  
        img_height = img.height
        print "img_height: "+str(img_height)  
        if img_width > img_height:
            ratio = float(dest_width / img_width) 
        else: ratio = float(dest_height / img_height) 

        img.resize(int(ratio*img_width), int(ratio *img.height))
        img.alpha_channel = False
        img.format = 'jpg'
        img.save(filename = thumb)
tadamhicks
  • 905
  • 1
  • 14
  • 34