Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render image from request in bottle

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>

like image 902
virupaksha Avatar asked Dec 27 '25 22:12

virupaksha


1 Answers

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)
like image 146
Daniel Roseman Avatar answered Dec 30 '25 23:12

Daniel Roseman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!