LoginSignup
0
0

PythonでAzure Blob Storage上の画像をメモリに直接読み込む

Posted at

Blobに配置した画像をNotebooksで触る必要があったので備忘録として。

import os
import matplotlib.pyplot as plt
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
from PIL import Image
from io import BytesIO

container_name = "container"
target_image_folder = "hoge/fuga"

STORAGE_ACCOUNT_CONNECTION_STRING = ""

blob_service_client = BlobServiceClient.from_connection_string(STORAGE_ACCOUNT_CONNECTION_STRING)

with blob_service_client:
    container_client = blob_service_client.get_container_client(container_name)
    blobs_list = []
    img_list = []
    for blob in container_client.list_blobs():
        blobs_list.append(blob)

    for blob in blobs_list:
        if os.path.dirname(blob.name) != target_image_folder:
            continue
        print("Downloading blob: ",blob.name)
        blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob.name)
        stream = blob_client.download_blob()
        data = stream.readall()
        img = Image.open(BytesIO(data))
        img_list.append(img)

print(len(img_list))
for img in img_list:
    plt.imshow(img)
    plt.show
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0