如何在Python中将图形列表转换为长png

2024-09-26 22:54:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个matplotlib图的列表,我想知道是否有任何方法可以将它们连接到一个长的png文件中。我试过用MagickWand做实验,但似乎没有办法将数字作为canvas.Image输入。你知道吗

我也尝试过用reportlab创建一个pdf,然后尝试用wand解释,但是每次我尝试在控制台中打开wand.image.Image(filename=pdf_file_name),它都会将None打印到输出中。你知道吗

有什么我忽略了,和/或有没有更好的方法来做到这一点。最好是PNG文件。你知道吗

提前谢谢!你知道吗


Tags: 文件方法image列表pdfpngmatplotlib数字
1条回答
网友
1楼 · 发布于 2024-09-26 22:54:33

假设所有的图都包含在一个PDF中,您可以使用遍历每个页面并将它们附加到一个PNG图像中。此技术可以根据需要修改为多个图形。你知道吗

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

相关问题 更多 >

    热门问题