简体   繁体   中英

Uploading array as a .jpg image to Azure blob storage

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.

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.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM