使用Python处理Azure Blob存储中的图像 [英] Use Python to process images in Azure blob storage

查看:44
本文介绍了使用Python处理Azure Blob存储中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 1000s of images sitting in a container on my blob storage. I want to process these images one by one in Python and spit out the new images out into a new container (the process is basically detecting and redacting objects). Downloading the images locally is not an option because they take up way too much space.

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

So far, I have been able to connect to the blob and have created a new container to store the processed images in, but I have no idea how to run the code to process the pictures and save them to the new container. Can anyone help with this?

到目前为止的代码是:

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 ,所以我不必下载图像吗?

Do I need to use get_blob_to_stream or get_blob_to_path from here: https://azure-storage.readthedocs.io/ref/azure.storage.blob.baseblobservice.html so I don't have to download the images?

任何帮助将不胜感激!

推荐答案

如注释中所述,您可能需要下载或流式处理blob,然后将结果处理后再上传到新容器中.

As mentioned in the comment, you may need to download or stream your blobs and then upload the results after processing them to your new container.

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

You could refer to the samples to download and upload blobs as below.

下载斑点:

# 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)

更新:

尝试按以下方式按流使用代码,有关更多详细信息,请参见两个链接: link2 (它们是相关问题,您可以一起看到它们).

Try to use the code by stream as below, for more details you could see the two links: link1 and link2 (they are related issue, you could see them together).

from azure.storage.blob import BlockBlobService
from io import BytesIO
from shutil import copyfileobj 
with BytesIO() as input_blob:
    with BytesIO() as output_blob:
        block_blob_service = BlockBlobService(account_name='my_account_name', account_key='my_account_key')
        # Download as a stream
        block_blob_service.get_blob_to_stream('mycontainer', 'myinputfilename', input_blob)

        # Do whatever you want to do - here I am just copying the input stream to the output stream
        copyfileobj(input_blob, output_blob)
        ...

        # Create the a new blob
        block_blob_service.create_blob_from_stream('mycontainer', 'myoutputfilename', output_blob)

        # Or update the same blob
        block_blob_service.create_blob_from_stream('mycontainer', 'myinputfilename', output_blob)

这篇关于使用Python处理Azure Blob存储中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆