作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个图像处理项目,其中我的图像保存在 Azure 上的 Blob 存储中。我的目标是读取 blob 图像并对它们应用一些转换,然后使用 python 将它们上传到单独的容器。目前,我可以从 Azure 读取图像,将图像转换为数组,以便我可以处理它们,但我无法将数组作为 .jpg 图像返回到 Azure。这就是我正在尝试的(其中 resized_image 是 (243, 387, 3) 数组):
resized_image = bytes(resized_image)
blob_service.create_blob_from_bytes("transformed", "test.jpg", resized_image)
这正在我的“转换后”容器中创建一个新文件,但它是空的且为 None 类型。
最佳答案
仅供引用,下面是我的示例代码,使用适用于 Python 和 OpenCV 的 Azure Blob 存储 SDK (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)
OpenCV imshow
显示源图像和调整大小的图像,如下图所示。
我从Azure Blob存储下载的源图像和调整大小的图像如下图所示。
关于python - 将数组作为 .jpg 图像上传到 Azure Blob 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59990859/
我是一名优秀的程序员,十分优秀!