3

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>

virupaksha
  • 363
  • 2
  • 11
  • How to access the raw image data in your bottle code depends on how exactly it's uploaded, whihc I can't tell from a quick scan of the link you provided. Try updating the code to dump / print the entire `request` object in that function, instead of opening it. You might find that's it's not in `requests.body` but somewhere else; or in a form-multipart format, etc. – Danielle M. Sep 30 '18 at 17:34
  • @DanielleM.Edited it – virupaksha Sep 30 '18 at 17:47

1 Answers1

1

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)
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895