Search code examples
pythonazure-blob-storagepng

Exporting and downloading images from Azure Blob Storage


I am exporting some matplotlib plots from Databricks to Blob Storage. No problem here, just using:

plt.savefig('/dbfs/my_plot.png')
dbutils.fs.cp('dbfs:my_plot.jpg', blob_container)

The problem is that when I download and open the image locally, it is blank. I am not sure if I have to transform it or at some point specify the metadata, cause it appears as application/octet-stream in the Blob.

any ideas?


Solution

  • • When you are trying to download and open the image locally, you are still seeing the image as blank because you are using the command as posted by you for saving them in readable image format in the Azure blob storage, which is correct but when you are showing the plot or downloading it, you should use the command ‘plt.show('/dbfs/my_plot.png')’ before the below commands as shown: -

     plt.show('/dbfs/my_plot.png') 
     plt.savefig('/dbfs/my_plot.png')
     dbutils.fs.cp('dbfs:my_plot.jpg', blob_container)
    

    If you are not using the above command as said, you will only get the image as blank only. Also, if you don’t want undesirable white space around your image, you can remove that using the command as below as this white space may also be the reason behind your image showing as blank as the space might have consumed greater area than the actual plot.

     plt.savefig('/dbfs/my_plot.png', bbox_inches='tight')
    

    For more information and details regarding the solution for this problem, kindly refer to the below community thread: -

    Save plot to image file instead of displaying it using Matplotlib