我想将图像numpy.ndarray数据上传到Azure存储Blob中。我正在使用BlobServiceClient。但是我找不到upload_blob接受numpy.ndarray的方法。如何上传?
blob_client = blob_service_client.get_blob_client(
container=CONTAINER, blob=filename)
blob_client.upload_blob(file)
您应该先将np.ndarray
编码为字节。
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)
资源:
1.使用PIL将Numpy数组转换为字节。