使用Python处理Azure Blob存储中的图像

时间:2019-03-14 19:27:29

标签: python-3.x azure azure-storage-blobs

我在Blob存储区的一个容器中有1000张图像。我想用Python一张一张地处理这些图像,然后将新图像吐出到一个新的容器中(该过程基本上是检测和编辑对象)。不能在本地下载图像,因为它们占用太多空间。

到目前为止,我已经能够连接到Blob,并创建了一个新容器来存储处理后的图像,但是我不知道如何运行代码来处理图片并将其保存到新容器中。有人可以帮忙吗?

到目前为止的代码是:

from azure.storage.file import FileService
from azure.storage.blob import BlockBlobService

# call blob service for the storage acct
block_blob_service = BlockBlobService(account_name = 'mycontainer', account_key = 'HJMEchn')

# create new container to store processed images
container_name = 'new_images'
block_blob_service.create_container(container_name)

我是否需要从此处使用get_blob_to_stream或get_blob_to_path: https://azure-storage.readthedocs.io/ref/azure.storage.blob.baseblobservice.html,所以我不必下载图像吗?

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

如评论中所述,您可能需要下载或流式处理Blob,然后将结果处理后再上传到新容器中。

您可以参考以下示例来下载和上传Blob。

Download the blobs

Kotlin

Upload blobs to the container

# Download the blob(s).
# Add '_DOWNLOADED' as prefix to '.txt' so you can see both files in Documents.
full_path_to_file2 = os.path.join(local_path, string.replace(local_file_name ,'.txt', '_DOWNLOADED.txt'))
print("\nDownloading blob to " + full_path_to_file2)
block_blob_service.get_blob_to_path(container_name, local_file_name, full_path_to_file2)

更新

尝试按如下所示按流使用代码,有关更多详细信息,您可以看到两个链接:link1link2(它们是相关问题,可以一起查看)。

# Create a file in Documents to test the upload and download.
local_path=os.path.expanduser("~\Documents")
local_file_name ="QuickStart_" + str(uuid.uuid4()) + ".txt"
full_path_to_file =os.path.join(local_path, local_file_name)

# Write text to the file.
file = open(full_path_to_file,  'w')
file.write("Hello, World!")
file.close()

print("Temp file = " + full_path_to_file)
print("\nUploading to Blob storage as blob" + local_file_name)

# Upload the created file, use local_file_name for the blob name
block_blob_service.create_blob_from_path(container_name, local_file_name, full_path_to_file)