如何直接使用Opencv从Azure blob存储中读取图像而不将其下载到本地文件?

时间:2017-06-16 11:41:13

标签: python-2.7 azure azure-storage-blobs opencv3.0

我想在Python 2.7中使用opencv 3从Azure blob存储中读取图像。 如何在不将blob下载到本地文件的情况下执行此操作?

1 个答案:

答案 0 :(得分:3)

根据我的经验,您可以尝试使用get_blob_to_bytes方法将blob作为字节数组下载并将其转换为opencv图像,如下面的示例代码所示。

from azure.storage.blob import BlockBlobService

account_name = '<your-storage-account>'
account_key = '<your accout key>'
block_blob_service = BlockBlobService(account_name, account_key)

container_name = 'mycontainer'
blob_name = 'test.jpg'
blob = block_blob_service.get_blob_to_bytes(container_name, blob_name)

import numpy as np
import cv2
# use numpy to construct an array from the bytes
x = np.fromstring(blob.content, dtype='uint8')

# decode the array into an image
img = cv2.imdecode(x, cv2.IMREAD_UNCHANGED)
print img.shape

# show it
cv2.imshow("Image Window", img)
cv2.waitKey(0)

希望它有所帮助。