Search code examples
pythonwand

Python - wand does not convert all the pages?


I'm trying to convert a PDF to jpg files. If I convert a big pdf file (~80 pages) Wand only convert the 22 first pages.

with Img(filename=file, resolution=300) as pic:
    library.MagickResetIterator(pic.wand)
    pic.scene = 1  # Start cpt of filename at 1 instead of 0
    pic.compression_quality = 100
    pic.background_color = Color("white")
    pic.alpha_channel = 'remove'
    pic.save(filename=(self.output_dir + '/result.jpg'))

I didn't understand why, any help would be great

Thanks


Solution

  • I am inclined to think that this is to do with how this operation works. When you open the .pdf file it holds the objects in memory and this is constrained by what's allowed by your system. This means that your memory does not allow any more than 22 pages. Loading 80 pdf pages take up huge memory space and your solution is not viable for such tasks.

    I would recommend something like this ( credit)

    Use pdf2image

    pip install pdf2image
    

    Then use it to get the image from the pdf

    from pdf2image import convert_from_path
    pages = convert_from_path('pdf_file', 500)
    
    for page in pages:
        page.save('out.png', 'png')