【问题标题】:How to Download a Blob Image from Python Azure Storage and create an Image object out of it?如何从 Python Azure 存储下载 Blob 图像并从中创建图像对象?
【发布时间】:2017-12-29 19:05:28
【问题描述】:

我有一个块 blob,它是一个 png 图像。当我将 blob 转换为字节并尝试创建图像时,我得到 没有足够的数据。 我是 python 和图像分析的新手。问题是如果图像分别为 2024*1512,则 blob 的大小为 1714562,宽度广告高度为 1714562。 下面是我检索图像的代码:

#Get Required image
def findImage(containerName, imageName): 
    return block_blob_service.get_blob_to_bytes(
            containerName, 
            input_folder + '/' + imageName, 
            max_connections=1).content; 

我必须按照如下所述来创建图像:

 np.array(imageToProcess).reshape(imageSize)

或者

PIL.Image.frombytes('L', imageSize, np.byte(imageToProcess), 'raw', 'L', 0, 1)
  • imageToProcess 是 findImage 函数的输出。

我试图让 blob 流式传输或路径仍然有同样的问题。需要将此 Blob 重新整形为实际图像大小并传递给另一个处理函数。 如果有人帮助我理解问题和可能的解决方案,我将不胜感激。

【问题讨论】:

    标签: python image azure png blob


    【解决方案1】:

    您从 Azure Blob 存储下载的 PNG 文件是 png 格式的二进制字节字符串,您需要使用 OpenCV 和 Numpy 将其转换为 numpy 数组或转换为 PIL.Image 对象。

    这是我使用 OpenCV 和 numpy 或 PIL.Image 的示例代码。

    1. 使用 OpenCV 和 numpy

      import numpy as np
      import cv2
      
      data = findImage(containerName, imageName)
      print type(data)
      # <type 'str'>
      nparr = np.fromstring(data, np.uint8)
      # if the numpy version is 2.x
      img_np = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR)
      # if the numpy version is 3.x
      img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
      print type(img_np)
      # <type 'numpy.ndarray'>
      
    2. 使用PIL.Image & StringIO

      from PIl import Image
      from StringIO import StringIO
      
      data = findImage(containerName, imageName)
      print type(data)
      # <type 'str'>
      img = Image.open(StringIO(data))
      print type(img)
      # <class 'PIL.PngImagePlugin.PngImageFile'>
      

    希望对你有帮助。

    【讨论】:

    • 非常感谢您的回答。我试过了,但是当我打印数据的类型不是 str 是字节时。现在我收到错误“无法将大小为 9180864 的数组重塑为形状 (2024,1512)”,然后它是大小为 1714562 的数组,这是 blob 的大小,现在是这个。有办法解决吗?
    • @sss PNG 是一种无损压缩图像文件格式,需要通过cv2.imdecode 将其转换为 RGB 像素数组,或者将其作为 PIL Image 对象读取。所以 PNG 文件的 blob 大小小于其真实的 RGB 像素数组大小(=2024*1512*3),您无法将小数组重塑为大数组。我不知道你为什么通过方法get_blob_to_bytes 获取blob 内容是bytes 类型,而不是str。如果您可以更新更多代码细节,我可以尝试帮助您修复它。
    • 正如我在文档中看到的,Blob 内容是 str 或 bytes。但我不知道为什么我没有得到 str 。除了我的 find image 函数和从 blob 创建图像并将其转换为 numpy 数组之外,我什么都没有。我应用了你的两个建议。问题是我的 Blob 大小为 1714562,而您的解决方案中的 img_np 返回大小为 9180864。此外,图像尺寸为 2024*1512。所以没有一个数组大小可以重新调整为实际图像大小。
    • 我通过将cv2.IMREAD_COLOR 更改为cv2.COLOR_GRAY2RGB 修复了blob 的下载,但是现在当我想将处理后的图像上传回存储时出现问题。我返回 numpy 数组并必须将新图像上传回存储。我的上传功能是def uploadProcessedImage(containerName, imageName, image): block_blob_service.create_blob_from_bytes( containerName, output_folder + '/' + imageName, image, index= 0, max_connections =1, content_settings= ContentSettings(content_type='image/png'))
    • 然后我将处理后的 numpy array.tobytes() 传递给我的 uploadProcessedImage 函数
    猜你喜欢
    • 2018-08-03
    • 2018-11-21
    • 1970-01-01
    • 2019-04-06
    • 1970-01-01
    • 2017-05-05
    • 2021-06-24
    • 2021-12-12
    • 1970-01-01
    相关资源
    最近更新 更多