Uploading array as a .jpg image to Azure blob storage

巧了我就是萌 提交于 2021-01-05 08:40:07

问题


I am working on an image processing project where my images are saved in blob storage on Azure. My goal is to read in the blob images and apply some transformations to them, then upload them to a separate container using python. Currently, I'm able to read in the images from Azure, convert the images to arrays so I can process them, but I'm having trouble getting the arrays back to Azure as .jpg images. This is what I'm trying (where resized_image is a (243, 387, 3) array):

resized_image = bytes(resized_image)
blob_service.create_blob_from_bytes("transformed", "test.jpg", resized_image)

This is creating a new file in my "transformed" container but it is empty and of None type.


回答1:


Just as reference, here is my sample code using Azure Blob Storage SDK for Python and OpenCV (pip install azure-storage-blob opencv-python) to download a blob image to resize and upload the resized one to Azure Blob.

from azure.storage.blob import BlockBlobService

account_name = '<your account name>'
account_key = '<your account key>'

blob_service = BlockBlobService(account_name, account_key)

container_name = '<your container name>'
blob_name = 'test_cat2.jpg' # my test image name
resized_blob_name = 'test_cat2_resized.jpg' # my resized image name

# Download image
img_bytes = blob_service.get_blob_to_bytes(container_name, blob_name)

# Resize image to 1/4 original size
import numpy as np
import cv2
src = cv2.imdecode(np.frombuffer(img_bytes.content, np.uint8), cv2.IMREAD_COLOR)
cv2.imshow("src", src)
(height, width, depth) = src.shape
dsize = (width//4, height//4)
tgt = cv2.resize(src, dsize)
cv2.imshow("tgt", tgt)
cv2.waitKey(0)

# Upload the resized image
_, img_encode = cv2.imencode('.jpg', tgt)
resized_img_bytes = img_encode.tobytes()
blob_service.create_blob_from_bytes(container_name, resized_blob_name, resized_img_bytes)

The OpenCV imshow shows the source and resized images as the figure below.

The source and resized images I download from Azure Blob Storage are as the figure below.



来源:https://stackoverflow.com/questions/59990859/uploading-array-as-a-jpg-image-to-azure-blob-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!