python - 使用 io.BytesIO 从 azure 存储读取图像?

标签 python python-3.x azure azure-blob-storage bytesio

我有一个带有一些图像的 azure blob 存储帐户。我想将它们作为字节读取以进行图像处理,这样我就可以将它们与 Microsoft 的 Face API 一起使用。我收到一条错误消息,告诉我需要一个“Bytes”对象,但我认为我已经将图像从 blob 格式转换为字节格式。我已经提供了我使用的整个代码,但问题来自最后一段代码

%matplotlib inline
import matplotlib.pyplot as plt
import io
from io import StringIO
import numpy as np
import cv2
from PIL import Image
#from StringIO import StringIO
from PIL import Image
import os
from array import array

我的凭据:

azure_storage_account_name = 'musicsurveyphotostorage'
azure_storage_account_key = None  # dont need key... we will access public 
blob... 

if azure_storage_account_name is None:
raise Exception("You must provide a name for an Azure Storage account")   
from azure.storage.blob import BlockBlobService
blob_service = BlockBlobService(azure_storage_account_name, 
azure_storage_account_key)

选择 blob 存储容器

#select container (folder) name where the files resides
container_name = 'musicsurveyphotostorage'

#list files in the selected folder
generator = blob_service.list_blobs(container_name)   
blob_prefix = 'https://{0}.blob.core.windows.net/{1}/{2}'

正在加载图像..错误来自以下代码块:

# load image file to process
blob_name = 'shiba.jpg'  #name of image I have stored
blob = blob_service.get_blob_to_bytes(container_name, blob_name)
image_file_in_mem = io.BytesIO(blob)
img_bytes = Image.open(image_file_in_mem)

我收到的错误消息如下:

TypeError   Traceback (most recent call last)
<ipython-input-13-6738e5733c01> in <module>()
 36 blob_name = 'shiba.jpg'  #name of image I have stored
 37 blob = blob_service.get_blob_to_bytes(container_name, blob_name)
**---> 38 image_file_in_mem = io.BytesIO(blob)**
 39 img_bytes = Image.open(image_file_in_mem)

TypeError: a bytes-like object is required, not 'Blob'

最佳答案

我手头没有帐户可以测试,但查看文档,get_blob_to_bytes()返回 Blob实例 - 要获取调用其 content 属性所需的实际字节,即:

blob = blob_service.get_blob_to_bytes(container_name, blob_name)
image_file_in_mem = io.BytesIO(blob.content)
img_bytes = Image.open(image_file_in_mem)
# ...

关于python - 使用 io.BytesIO 从 azure 存储读取图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49684211/

相关文章:

python - Selenium Python Firefox webdriver : can't modify profile

linux - 如何在不使用 PyCharm 的情况下运行我的程序?

python - 在pandas python中按列号读取csv文件

azure 应用程序洞察: troubleshoot size messages

python - 当我安装 Jinja2 时 "the speedup extension could not be compiled, Jinja will fall back to the native python classes"是什么意思?

python - 是否可以在 Google AppEngine 上使用 django Piston?

sql-server - Robot Framework - 尝试连接到云中的 SQL 服务器 (Azure) 时出错

azure - 重置 Azure 存储队列消息的可见性

python - 将新的 ManyToManyField 默认值追溯设置为现有模型

python - TensorFlow Estimator 1.3 无法获取 predict_proba?