1

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)
JBA
  • 219
  • 4
  • 15

1 Answers1

1

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.

Livne Rosenblum
  • 196
  • 1
  • 12