how to convert return binary data from azure blob storage back to image using python

Joshua Cezar C. Claracay 21 Reputation points
2022-09-23T02:46:29.673+00:00

so this is my code for downloading the image from azure blob storage container
244087-dataimg.png

and here's the return data that i got
244077-returndata.png

can anyone help me in converting the return data back to image format?

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,192 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,931 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruno Lucas 4,436 Reputation points MVP
    2022-09-23T04:29:03.417+00:00

    Hi @Joshua Cezar C. Claracay

    Do you just want to display or save it somewhere?

    To display you need something like matplotlib:

    from azure.storage.blob import BlobServiceClient  
    from matplotlib import pyplot as plt  
    from matplotlib import image as mpimg  
    import io  
      
      
    STORAGEACCOUNTURL = "[your blob url]"  
    STORAGEACCOUNTKEY = "[storage key]"  
    CONTAINERNAME = "[your container name]"  
    BLOBNAME = "[image file name]"  
      
    blob_service_client_instance = BlobServiceClient(  
        account_url=STORAGEACCOUNTURL, credential=STORAGEACCOUNTKEY)  
      
    blob_client_instance = blob_service_client_instance.get_blob_client(  
        CONTAINERNAME, BLOBNAME, snapshot=None)  
      
    blob_data = blob_client_instance.download_blob()  
    data = blob_data.readall()  
      
    fp = io.BytesIO(data)  
      
    with fp:  
        img = mpimg.imread(fp, format='jpeg')  
    plt.imshow(img)  
    plt.show()  
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Dillon Silzer 57,826 Reputation points Volunteer Moderator
    2022-09-23T03:07:07.98+00:00

    Hi @Joshua Cezar C. Claracay

    See the following similar topic at https://stackoverflow.com/questions/4004710/how-to-decode-this-yuv-colorspace-string-and-save-it-as-an-image

    A suggested solution (by Scott Griffiths) was:

    def clip(v):  
        # Clip to 0-255  
        v = max(v, 0)  
        v = min(v, 255)  
        return v  
      
    def yuvToRgb(y, u, v):  
        c = y - 16  
        d = u - 128  
        e = v - 128  
        R = (298 * c) + (409 * e) + 128  
        G = (298 * c) - (100 * d) - (208 * e) + 128  
        B = (298 * c) + (516 * d) + 128  
        R >>= 8  
        G >>= 8  
        B >>= 8  
        return (clip(R), clip(G), clip(B))  
      
    b = bytearray('\x84K\x7f\x86K\x7f\x86G\x7f~K\x7f~I}\x85K}\x85') # etc...  
    RGB = []  
    for i in xrange(0, len(b), 3):  
        RGB.append(yuvToRgb(b[3*i], b[3*i+1], b[3*i+2]))  
    

    More information on YUV: https://en.wikipedia.org/wiki/YUV

    A way to convert YUV (NV12) files to RGB (BMP). Requires PIL (Python Image Library)

    https://gist.github.com/fzakaria/2472889

    -------------------------------------

    If this is helpful please accept answer.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.