问题
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