将阵列作为.jpg映像上载到Azure blob存储

2024-06-01 07:22:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从事一个图像处理项目,我的图像保存在Azure上的blob存储中。我的目标是读入blob图像并对其应用一些转换,然后使用python将它们上传到单独的容器中。目前,我可以从Azure读取图像,将图像转换为数组,以便处理它们,但我无法将数组作为.jpg图像返回Azure。这就是我正在尝试的(其中,调整大小的_图像是一个(2433873)数组):

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

这是在我的“转换”容器中创建一个新文件,但它是空的,没有类型


Tags: 项目from图像image目标bytescreateservice
1条回答
网友
1楼 · 发布于 2024-06-01 07:22:55

作为参考,这里是我使用Azure Blob Storage SDK for Python和OpenCV(pip install azure-storage-blob opencv-python)下载Blob图像以调整大小并将调整大小的图像上载到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)

OpenCVimshow显示源图像和调整大小的图像,如下图所示

enter image description here

我从Azure Blob存储下载的源图像和调整大小的图像如下图所示

enter image description here

相关问题 更多 >