JBA
JBA

Reputation: 237

How to upload a numpy.ndarray image into Azure Storage Blob

I would like to upload an image numpy.ndarray data into Azure Storage Blob. I am using the BlobServiceClient. But I cannot find a way for upload_blob to accept a numpy.ndarray. How can I upload it?

     blob_client = blob_service_client.get_blob_client(
         container=CONTAINER, blob=filename)

     blob_client.upload_blob(file)

Upvotes: 1

Views: 591

Answers (1)

Livne Rosenblum
Livne Rosenblum

Reputation: 306

You should encode your np.ndarray into bytes first.

import numpy as np
from azure.storage.blob import BlockBlobService
from PIL import Image
account_name = '<your account name>'
account_key = '<your account key>'
container_name = '<your container name>'
blob_name = 'image.jpg'  # my test image name
img: np.ndarray = []  # Load your image.
im = Image.fromarray(img)
img_byte_arr = io.BytesIO()
im.save(img_byte_arr, format='jpeg')
img_byte_arr = img_byte_arr.getvalue()
blob_service = BlockBlobService(account_name, account_key)
blob_service.create_blob_from_bytes(container_name, blob_name, img_byte_arr)

Resources:

1. Using PIL to convert Numpy array into bytes.

Upvotes: 1

Related Questions