1

I need to convert PDFs with multiple pages to a single unanimated gif where the pages will be one below the other using Python 3 and ideally Wand. I have tried doing this with the following code, however, the gif is still animated so that individual pages behave like frames of an animation.

Here is my current code: (PDFfile is the location of a PDF, GIFfile is the location that I want to save the unanimated gif)

from wand.image import Image

with Image(filename = PDFfile) as img:
    Pages = []
    for x in range(len(img.sequence)):
        Frame = Image(image = img.sequence[x])
        Pages.append(Frame)
    CombinedImage = Image()
    HeightToPlaceAt = 0
    for Page in Pages:
        CombinedImage.blank(Pages[0].width,Pages[0].height * len(Pages))
        CombinedImage.composite(Page,0,HeightToPlaceAt)
        HeightToPlaceAt +=  Page.height
    CombinedImage.save(filename = GIFfile)

How can I stop the animation so that all the frames are visible at once, is there a way to 'flatten' the image down to one frame?

Thanks

ECPeasy
  • 71
  • 4
  • First time I've ever heard the term "*un*animated GIF". Are you sure you don't simply want one image per page? – Jongware Nov 12 '16 at 19:49
  • Using Python I need to display several image filetypes, including PDFs in a text widget. I thought the easiest way to do this would be to convert them all into a .gif, .ppm or .pgm. I have been able to turn other filetypes into .gif but multipage pdfs end up being animated so yeah, I need all the pages in one frame as far as I can see. – ECPeasy Nov 13 '16 at 13:54

1 Answers1

2

Sometimes it helps to just read through your for loops before presuming that there must be a functions that you don't know about that will fix your problem. Looks like I have found this out the hard way. My amended code:

with Image(filename = PDFfile) as img:
    Pages = []
    for x in range(len(img.sequence)):
        Frame = Image(image = img.sequence[x])
        Pages.append(Frame)
    CombinedImage = Image()
    CombinedImage.blank(Pages[0].width,Pages[0].height * len(Pages))
    HeightToPlaceAt = 0
    for Page in Pages:
        CombinedImage.composite(Page,0,HeightToPlaceAt)
        HeightToPlaceAt +=  Page.height
    CombinedImage.save(filename = GIFfile)
ECPeasy
  • 71
  • 4