内联保存图像 (Base64) 以在 Jupyter 笔记本中导出 Markdown

Save Images inline (Base64) for Markdown export in Jupyter notebook

如何从 Matplotlib 获取图像:

plt.tight_layout()
plt.savefig('Image.png', facecolor='w', edgecolor='w', transparent=False, bbox_inches='tight', pad_inches=0.1)
plt.show()

内联保存(嵌入为 base64?),因此不需要外部文件, Notebook 何时下载为 Markdown 文件?

我的解决方案:

安装这个:https://github.com/freeman-lab/embed-images
还有这个:https://nbconvert.readthedocs.io/en/latest/install.html

添加到第一个单元格:

%matplotlib inline
import os 
import ipyparams

最后一个单元格:

os.system('jupyter nbconvert --to markdown ' + ipyparams.notebook_name)
os.system('embed-images ' + ipyparams.notebook_name[:-6] + '.md > ' + ipyparams.notebook_name[:-6] + '_emb.md')

NBconvert 将 notebook.ipynb 转换为 notebook.md 以及文件夹 中的所有图像notebook_files.
Embed-Images 将这些图像转换为 base64,将它们的代码插入文件中并将其保存为 notebook_emb.md

两条线都输出“0”表示转换成功。