Why does reading in an image in PIL(low) break Flask image end point? Why does reading in an image in PIL(low) break Flask image end point? flask flask

Why does reading in an image in PIL(low) break Flask image end point?


You don't have any problems with Pillow. Your problem is that you are serving an empty response. If you are serving a thumbnail, you ask Pillow to read the file:

if desiredWidthStr or desiredHeightStr:        print 'THUMBNAIL'    im = Image.open(userDoc.file_)  # reads from the file object

You then try and serve from that same file object:

if desiredWidthStr or desiredHeightStr:        print 'THUMBNAIL'    # Load the image in Pillow    im = Image.open(userDoc.file_)  # <=== THE PROBLEM!!!    # TODO: resize and save the image in /tmpreturn Response(userDoc.file_.read(), mimetype=userDoc.file_.content_type)

The userDoc.file_.read() here will return, at best, a partial image since Image.open() has already moved the file pointer. It depends on the image type how much is actually read and where the image pointer is located at by that time.

Add in a file.seek() call and you'll see your image re-appear:

if desiredWidthStr or desiredHeightStr:        print 'THUMBNAIL'    # Load the image in Pillow    im = Image.open(userDoc.file_)print 'NORMAL'userDoc.file_.seek(0)  # ensure we are reading from the startreturn Response(userDoc.file_.read(), mimetype=userDoc.file_.content_type)