Skip to content Skip to sidebar Skip to footer

Pil Unidentifiedimageerror From Azure Blob Trigger Though Image Opens In 'watch'

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: f

Solution 1:

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


defmain(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 :

{"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"}]}

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:

Upload a big image: enter image description here

resize the image and save it to another container: enter image description here

Solution 2:

It turns out setting a breakpoint at the Image.open(blobin) line breaks the function somehow. Removing it from there and adding it to the next line does not prompt the error anymore. Probably Azure doesn't like to wait and times out the stream? Who knows.

Post a Comment for "Pil Unidentifiedimageerror From Azure Blob Trigger Though Image Opens In 'watch'"