gpt4 book ai didi

azure - 尽管图像在 'watch' 中打开,但 Azure Blob 触发的 PIL UnidentifiedImageError

转载 作者:行者123 更新时间:2023-12-03 01:26:02 26 4
gpt4 key购买 nike

我正在尝试使用 Blob 触发器在本地调试 Azure 函数。将图像文件上传到 Azure 时,本地运行的函数会接收触发器。

def main(blobin: func.InputStream, blobout: func.Out[bytes], context: func.Context):
logging.info(f"Python blob trigger function processed blob \n"
f"Name: {blobin.name}\n"
f"Blob Size: {blobin.length} bytes")

image_file = Image.open(blobin)

我的函数.json

{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "blobin",
"type": "blobTrigger",
"direction": "in",
"path": "uploads/{name}",
"connection": "STORAGE"
},
{
"name": "blobout",
"type": "blob",
"direction": "out",
"path": "uploads/{blob_name}_resized.jpg",
"connection": "STORAGE"
}
]
}

Image.open(blobin)行运行时出现的错误是:

System.Private.CoreLib: Exception while executing function:Functions.ResizePhoto. System.Private.CoreLib: Result: FailureException: UnidentifiedImageError: cannot identify image file<_io.BytesIO object at 0x0000017FD4FD7F40>

有趣的是,图像本身确实在 VSCode 监 window 口中打开,但在运行代码时失败。如果我再次将其添加到 watch 中(可能会触发 watch 刷新),它也会给出与上面相同的错误。

VSCode watch

最佳答案

如果您想调整图像大小,然后通过函数 blob 触发器保存它,请尝试以下代码:

import logging
from PIL import Image
import azure.functions as func
import tempfile
import ntpath
import os


def main(blobin: func.InputStream, blobout:func.Out[func.InputStream], context: func.Context):
logging.info(f"Python blob trigger function processed blob \n"
f"Name: {blobin.name}\n"
f"Blob Size: {blobin.length} bytes")

temp_file_path = tempfile.gettempdir() + '/' + ntpath.basename(blobin.name)
print(temp_file_path)
image_file = Image.open(blobin)
image_file.resize((50,50)).save(temp_file_path)

blobout.set(open(temp_file_path, "rb").read())

os.remove(temp_file_path)

函数.json:

{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "blobin",
"type": "blobTrigger",
"direction": "in",
"path": "samples-workitems/{name}",
"connection": "STORAGE"
},
{
"name": "blobout",
"type": "blob",
"direction": "out",
"path": "resize/{name}",
"connection": "STORAGE"
}
]
}

请注意,您不应将调整大小的图像存储在同一个容器中,因为这会导致无限循环(新图像触发 blob 触发器并一次又一次调整大小),并且您的问题是由于新调整大小的图像输出不正确所致这样运行时就会出现异常:Image.open(blobin)

无论如何,上面的代码非常适合我,请参阅下面的结果:

上传大图: enter image description here

调整图像大小并将其保存到另一个容器: enter image description here

关于azure - 尽管图像在 'watch' 中打开,但 Azure Blob 触发的 PIL UnidentifiedImageError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66251887/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com