code: def save_png_with_metadata(fig, filename, fig_kwds, kwds):
      
      from PIL import Image, PngImagePlugin
      fig.savefig(filename, **fig_kwds)
  
      im = Image.open(filename)
      meta = PngImagePlugin.PngInfo()
  
      for key in kwds:
          meta.add_text(str(key), str(kwds[key]))
  
      im.save(filename, "png", pnginfo=meta)
docs: Save a matplotlib figure to a png with metadata