Render image from request in bottle

186 views Asked by At

My intention is to upload an image and do some image processing. For now, I intend to render the uploaded image.

I used the code here to build my front end and I wrote the backend in python using bottle, which is as follows:

@route('/test', method='POST')
def serve_image():
    # import pdb; pdb.set_trace()
    image = Image.open(request.body)
    image.show()

I get an errors as follows

OSError: cannot identify image file <_io.BytesIO object at 0x0000017386B53A40>

What am I missing?

EDIT: When I print the whole request, this is what I get

< http://localhost:8080/test>

1

There are 1 answers

0
Daniel Roseman On BEST ANSWER

That tutorial is not very comprehensive, but the full documentation is more useful:

The image data is uploaded as part of a standard multipart form post, and included as a form element named webcam.

So rather than trying to pass the whole request body to Pillow, you need to pass just that element, using the request.files multidict, and accessing its file attribute to get the buffer:

image = Image.open(request.files['webcam'].file)