Search code examples
pythonpython-3.xazureazure-blob-storagebytesio

reading an image from azure storage using io.BytesIO?


I have an azure blob storage account with some images. I want to read them as bytes for imaging processing, so I can use them with Microsoft's Face API. I am getting an error message telling me I need a "Bytes" object, but I thought I was already converting the image from blob to byte format. I have provided the entire code I used, but the issue comes from the very last chunk of code

%matplotlib inline
import matplotlib.pyplot as plt
import io
from io import StringIO
import numpy as np
import cv2
from PIL import Image
#from StringIO import StringIO
from PIL import Image
import os
from array import array

my credentials:

azure_storage_account_name = 'musicsurveyphotostorage'
azure_storage_account_key = None  # dont need key... we will access public 
blob... 

if azure_storage_account_name is None:
raise Exception("You must provide a name for an Azure Storage account")   
from azure.storage.blob import BlockBlobService
blob_service = BlockBlobService(azure_storage_account_name, 
azure_storage_account_key)

select the blob storage container

#select container (folder) name where the files resides
container_name = 'musicsurveyphotostorage'

#list files in the selected folder
generator = blob_service.list_blobs(container_name)   
blob_prefix = 'https://{0}.blob.core.windows.net/{1}/{2}'

loading the image..the error comes from this following block of code:

# load image file to process
blob_name = 'shiba.jpg'  #name of image I have stored
blob = blob_service.get_blob_to_bytes(container_name, blob_name)
image_file_in_mem = io.BytesIO(blob)
img_bytes = Image.open(image_file_in_mem)

the error message I get is the following:

TypeError   Traceback (most recent call last)
<ipython-input-13-6738e5733c01> in <module>()
 36 blob_name = 'shiba.jpg'  #name of image I have stored
 37 blob = blob_service.get_blob_to_bytes(container_name, blob_name)
**---> 38 image_file_in_mem = io.BytesIO(blob)**
 39 img_bytes = Image.open(image_file_in_mem)

TypeError: a bytes-like object is required, not 'Blob'

Solution

  • I don't have an account at hand to test but looking at the docs, get_blob_to_bytes() returns a Blob instance - to get the actual bytes you need to invoke its content property, i.e.:

    blob = blob_service.get_blob_to_bytes(container_name, blob_name)
    image_file_in_mem = io.BytesIO(blob.content)
    img_bytes = Image.open(image_file_in_mem)
    # ...