[英]matplotlib setting pdf raster resolution using pdfpages savefig dpi
我有一个项目,该项目使用matplotlib生成了一组图表,并将每个图表导出为一个单独的PNG文件。 客户还希望将图表组输出为单个PDF文件。 我们已经成功使用PIL图像和matplotlib PdfPages在单个页面上导入每个PNG并导出多页PDF的情况下成功使用,但是PDF的输出质量是不可接受的。 如果我将dpi = 100以外的任何值与savefig一起使用,则输出为空白。 关于如何导出具有更高栅格分辨率的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()
输出为空白,因为图像位于画布之外。 对于dpi = 200且不调整原始图像大小的情况,我们需要将figimage修改为:
fig.figimage(im, 0, -1080, zorder = 1)
输出对y值非常敏感,与-1080相比变化很小,图像放置在页面顶部或底部。 我不明白为什么dpi设置会对figimage y参数产生这种影响,但是现在可以产生高质量的PDF输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.