简体   繁体   English

matplotlib使用pdfpages savefig dpi设置pdf光栅分辨率

[英]matplotlib setting pdf raster resolution using pdfpages savefig dpi

I have a project that generates a set of charts using matplotlib and exports each one as an individual PNG file. 我有一个项目,该项目使用matplotlib生成了一组图表,并将每个图表导出为一个单独的PNG文件。 The client also wants the group of charts output as a single PDF file. 客户还希望将图表组输出为单个PDF文件。 We have successfully got this to work using PIL Image and matplotlib PdfPages to import each PNG on a separate page and export the multi-page PDF, but the PDF output quality is unacceptable. 我们已经成功使用PIL图像和matplotlib PdfPages在单个页面上导入每个PNG并导出多页PDF的情况下成功使用,但是PDF的输出质量是不可接受的。 If I use anything other than dpi=100 with savefig, then output is blank. 如果我将dpi = 100以外的任何值与savefig一起使用,则输出为空白。 Any suggestions on how to export a PDF with higher raster resolution? 关于如何导出具有更高栅格分辨率的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()

The output was blank because the image was positioned off of the canvas. 输出为空白,因为图像位于画布之外。 For dpi=200 and no resizing of the original image, we needed to modify figimage to: 对于dpi = 200且不调整原始图像大小的情况,我们需要将figimage修改为:

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

The output is very sensitive to the y-value, a small change from -1080 and the image was placed off the top or bottom of the page. 输出对y值非常敏感,与-1080相比变化很小,图像放置在页面顶部或底部。 I don't understand why the dpi setting has this effect on the figimage y parameter, but this now produces PDF output with good quality. 我不明白为什么dpi设置会对figimage y参数产生这种影响,但是现在可以产生高质量的PDF输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM