我有一个项目,它使用matplotlib生成一组图表,并将每个图表导出为单独的PNG文件。客户端还希望将这组图表输出为单个PDF文件。我们已经成功地使用PIL Image和matplotlib将每个PNG导入到单独的页面上并导出多页PdfPages,但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()发布于 2014-02-20 02:03:47
输出为空,因为图像位于画布之外。对于dpi=200并且没有调整原始图像的大小,我们需要将figimage修改为:
fig.figimage(im, 0, -1080, zorder = 1)输出对y值非常敏感,从-1080略有变化,图像被放置在页面的顶部或底部。我不明白为什么dpi设置对figimage y参数有这样的影响,但这现在可以产生高质量的PDF输出。
https://stackoverflow.com/questions/21887491
复制相似问题