Cell magic to save image output as a .png file

Hey @krassowski, thanks for taking time out of your day and making this implementation, I really appreciate your effort!

I think having a cell magic like this has really the potential to improve the workflow in Jupyter for lots of users when it comes to saving images.

Therefore, some thoughts from my side:

  • Would it be possible to make a package out of your implementation?
  • Or would it be possible to add this feature to an already existing library (e.g. IPython or Ipywidgets)?
  • Saving the output with %%capture_png does not display the image in Jupyter. That is also the case when using the %%capture magic.
    I think it would be more intuitive behavior if cell output is still shown by default, but can be hidden with an extra parameter like --hide_cell_output. I did not find an option to hide/show cell output in Jupyter here Built-in magic commands — IPython 7.30.0 documentation, but maybe this would be also possible to implement?
    EDIT: yes, that is possible by simply adding:
    for output in result.outputs:
        display(output)
  • the current implementation has a dependency to PIL, maybe it is also possible to save the BytesIO object to a png file without PIL?

  • EDIT 2: to avoid overwriting of images, one could also add an option that includes the current time of the cell execution to this magic:

 path = out_paths.pop(0)
 path = path.split(".png")[0] + str(time.time_ns()) + ".png"
1 Like