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