【问题标题】:The Image file could not be opened Azure Blob Storage无法打开图像文件 Azure Blob 存储
【发布时间】:2021-05-06 07:47:21
【问题描述】:

您好,我编写了一个将图像直接上传到 Azure Blob 存储的爬虫

上传成功,但打开文件时出现错误“无法打开图像文件” 在本地保存时它工作正常,所以在转换为字节上传到 blob 时它会中断。请帮忙

我已尝试根据注释掉的代码将其转换为字符串,但这也不起作用 - 提前非常感谢

block_blob_service = BlockBlobService(account_name=AZURE_ACCOUNT_NAME, account_key=AZURE_ACCOUNT_KEY, connection_string=AZURE_CONNECTION_STRING)

r = requests.get(image_url, stream=True)
image_name='test_thumb.jpg'
output = io.BytesIO()
r.raw.decode_content = True
with Image.open(r.raw) as image:
    image.thumbnail((100, 100), Image.ANTIALIAS)
    image.save(image_name, format="JPEG")
#     thumbnail_as_string = base64.b64encode(output.getvalue())
    block_blob_service.create_blob_from_bytes(container_name="media", blob_name=image_name, blob=image.tobytes())```

【问题讨论】:

    标签: python python-imaging-library azure-blob-storage django-storage


    【解决方案1】:

    已解决

    block_blob_service = BlockBlobService(account_name=AZURE_ACCOUNT_NAME, account_key=AZURE_ACCOUNT_KEY, connection_string=AZURE_CONNECTION_STRING)
    def create_thumbnail(image_url, image_name):
        r = requests.get(image_url, stream=True)
        image_stream = io.BytesIO()
        r.raw.decode_content = True
        with Image.open(r.raw) as image:
            image.thumbnail((200, 200), Image.ANTIALIAS)
            image.save(image_stream, format="JPEG")
            image_stream.seek(0)
            block_blob_service.create_blob_from_bytes(container_name="media", blob_name=image_name, blob=image_stream.read())
    

    【讨论】:

      猜你喜欢
      • 2012-01-23
      • 2019-04-04
      • 2020-10-08
      • 2016-02-05
      • 2018-07-11
      • 2019-04-03
      • 2015-09-04
      • 2021-07-09
      • 2013-07-29
      相关资源
      最近更新 更多