【问题标题】:Uploading array as a .jpg image to Azure blob storage将数组作为 .jpg 图像上传到 Azure Blob 存储
【发布时间】:2020-01-31 04:58:12
【问题描述】:

我正在处理一个图像处理项目,我的图像保存在 Azure 上的 blob 存储中。我的目标是读取 blob 图像并对它们应用一些转换,然后使用 python 将它们上传到单独的容器中。目前,我能够从 Azure 中读取图像,将图像转换为数组以便处理它们,但是我无法将数组作为 .jpg 图像返回 Azure。这就是我正在尝试的(其中 resized_image 是一个 (243, 387, 3) 数组):

resized_image = bytes(resized_image)
blob_service.create_blob_from_bytes("transformed", "test.jpg", resized_image)

这是在我的“转换”容器中创建一个新文件,但它是空的并且是 None 类型。

【问题讨论】:

    标签: python azure image-processing azure-blob-storage


    【解决方案1】:

    作为参考,这是我使用 Azure Blob Storage SDK for Python 和 OpenCV (pip install azure-storage-blob opencv-python) 下载 blob 图像以调整大小并将调整后的图像上传到 Azure Blob 的示例代码。

    from azure.storage.blob import BlockBlobService
    
    account_name = '<your account name>'
    account_key = '<your account key>'
    
    blob_service = BlockBlobService(account_name, account_key)
    
    container_name = '<your container name>'
    blob_name = 'test_cat2.jpg' # my test image name
    resized_blob_name = 'test_cat2_resized.jpg' # my resized image name
    
    # Download image
    img_bytes = blob_service.get_blob_to_bytes(container_name, blob_name)
    
    # Resize image to 1/4 original size
    import numpy as np
    import cv2
    src = cv2.imdecode(np.frombuffer(img_bytes.content, np.uint8), cv2.IMREAD_COLOR)
    cv2.imshow("src", src)
    (height, width, depth) = src.shape
    dsize = (width//4, height//4)
    tgt = cv2.resize(src, dsize)
    cv2.imshow("tgt", tgt)
    cv2.waitKey(0)
    
    # Upload the resized image
    _, img_encode = cv2.imencode('.jpg', tgt)
    resized_img_bytes = img_encode.tobytes()
    blob_service.create_blob_from_bytes(container_name, resized_blob_name, resized_img_bytes)
    

    OpenCV imshow 显示源图像和调整大小的图像,如下图所示。

    我从 Azure Blob Storage 下载的源图片和调整大小的图片如下图。

    【讨论】:

      猜你喜欢
      • 2018-12-17
      • 1970-01-01
      • 2020-01-25
      • 2021-01-16
      • 2020-01-09
      • 2020-06-07
      • 2014-07-22
      • 2020-02-23
      • 1970-01-01
      相关资源
      最近更新 更多