简体   繁体   中英

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>

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM