https://github.com/als-computing/scicat_beamline/blob/4ae5edf51f73fe9741122325caca16e4da3f39db/scicat_beamline/scicat_utils.py#L82
This might be enhanced at some point by saving to memory rather than using disk:
# Create an in-memory binary stream
buf = io.BytesIO()
# Save the figure to the buffer in a desired format (e.g., 'png')
plt.savefig(buf, format='png')
# Important: Seek to the beginning of the buffer before reading
buf.seek(0)
https://github.com/SciCatProject/pyscicat/blob/main/pyscicat/client.py#L835
This should probably be split into two methods, encode_image_file and encode_image, with the first calling the second, which accepts a buffer.