无法打开图像文件 Azure Blob 存储

问题描述

嗨,我编写了一个将图像直接上传到 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())```

解决方法

已解决

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())