0

I have a list of matplotlib figures and I'm wondering if there's any way to concatenate them into a single long png file. I have tried experimenting with MagickWand but it doesn't seem like there is a way to input figures as a canvas.Image.

As well I have tried creating a pdf with reportlab and then try to interpret with wand but every time I try to open up wand.image.Image(filename=pdf_file_name) in the console, it prints None to the output.

Is there anything that I am overlooking, and/or is there a better way to do this. Preferably this is a PNG file.

Thank you in advance!

dlwlrma
  • 99
  • 2
  • 13
  • Take a look [here](http://stackoverflow.com/questions/10647311/how-do-you-merge-images-using-pil-pillow) – Mauro Baraldi Feb 27 '16 at 00:01
  • @MauroBaraldi I am using PDF files, not jpg or png files as the input. Moreover, PIL doesn't fully support PDF files. – dlwlrma Feb 29 '16 at 13:30

1 Answers1

0

Assuming that all the figures are contained within a single PDF, you can use to iterate over each page & append them to a single PNG image. This technique can be modified for multiple figures as needed.

from wand.image import Image

pdf_file='/path/to/figures.pdf'

# Read figures
with Image(filename=pdf_file) as figures:
  # Create blank image with the dimensions of all pages/figures
  with Image(width= figures.width,
             height=figures.height*len(figures.sequence)) as canvas:
    top=0                            # Position offset
    for page in figures.sequence:    # Iterate over each page in figures
      canvas.composite(page, 0, top) # Composite page on to canvas
      top += page.height             # Adjust offset
   canvas.save(filename="out.png")   # Write out long png
emcconville
  • 23,800
  • 4
  • 50
  • 66