尽管图像在“监视"中打开,但来自Azure Blob触发器的PIL UnidentifiedImageError [英] PIL UnidentifiedImageError from Azure Blob Trigger though image opens in 'watch'

查看:77
本文介绍了尽管图像在“监视"中打开,但来自Azure Blob触发器的PIL UnidentifiedImageError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to debug an Azure function locally using Blob trigger. When uploading an image file to Azure, the trigger is received by my function running locally.

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)

我的function.json

My function.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)行运行时出现的错误是:

The error I get when the Image.open(blobin) line runs is :

System.Private.CoreLib:执行函数时发生异常:Functions.ResizePhoto.System.Private.CoreLib:结果:失败异常:UnidentifiedImageError:无法识别图像文件< _io.BytesIO对象位于0x0000017FD4FD7F40>

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

有趣的是,图像本身确实在VSCode监视窗口中打开,但是在运行代码时失败.如果我再次将其添加到手表中,也会产生与上述相同的错误(可能触发手表刷新).

The interesting thing is that the image itself does open in VSCode watch window, but fails when the code is ran. It also gives the same error as above if I add it to the watch again (probably triggering a watch refresh).

推荐答案

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

If you want to resize an image and then save it by function blob trigger, try the code below:

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)

function.json:

function.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)

Note that you should not store the resized image in the same container as it will lead to an endless loop (new image triggers the blob trigger and resize again and again) and your issue is due to the newly resized image outputted not correctly so that the exception occors while run : Image.open(blobin)

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

Anyway, the code above works for me perfectly, see the result below:

上传大图:

调整图像大小并将其保存到另一个容器中:

resize the image and save it to another container:

这篇关于尽管图像在“监视"中打开,但来自Azure Blob触发器的PIL UnidentifiedImageError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆