Python 3 Wand How to make an unanimated gif from multiple PDF pages

前提是你 提交于 2019-12-11 07:35:04

问题


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


回答1:


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)


来源:https://stackoverflow.com/questions/40566955/python-3-wand-how-to-make-an-unanimated-gif-from-multiple-pdf-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!