【问题标题】:matplotlib setting pdf raster resolution using pdfpages savefig dpimatplotlib 使用 pdfpages savefig dpi 设置 pdf 光栅分辨率
【发布时间】:2014-03-20 04:43:32
【问题描述】:

我有一个项目,它使用 matplotlib 生成一组图表并将每个图表导出为单独的 PNG 文件。客户还希望将一组图表输出为单个 PDF 文件。我们已经成功地使用 PIL Image 和 matplotlib PdfPages 在单独的页面上导入每个 PNG 并导出多页 PDF,但 PDF 输出质量是不可接受的。如果我在 savefig 中使用 dpi=100 以外的任何内容,则输出为空白。有关如何以更高的光栅分辨率导出 PDF 的任何建议?

  pp = PdfPages(filepath+'Treatment Zone Charts.pdf')

  for file in os.listdir(filepath):
      if "Treatment Zone Charts" in file and file.endswith(".png"):
          print filepath+file

          #read PNG image
          im = Image.open(filepath+'/'+file)
          im = im.resize((1100,850),Image.ANTIALIAS)

          im = np.array(im).astype(np.float) / 255

          # Create a figure with size w,h tuple in inches
          fig = Figure(figsize=(11,8.5))
          # Create a canvas and add the figure to it.
          canvas = PDF_FigureCanvas(fig)

          #add image to plot
          fig.figimage(im, 0, -220, zorder = 1)

          # Save the Plot to the PDF file
          pp.savefig(fig, dpi=100)

  #close the PDF file after the last plot is created
  pp.close()

【问题讨论】:

    标签: python pdf matplotlib


    【解决方案1】:

    输出为空白,因为图像位于画布之外。对于 dpi=200 且不调整原图大小的情况,我们需要将 figimage 修改为:

              fig.figimage(im, 0, -1080, zorder = 1)
    

    输出对 y 值非常敏感,从 -1080 的微小变化,图像被放置在页面顶部或底部之外。我不明白为什么 dpi 设置会对 figimage y 参数产生这种影响,但是现在这会产生高质量的 PDF 输出。

    【讨论】:

      猜你喜欢
      • 2014-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 2014-09-29
      相关资源
      最近更新 更多