Using a PIL Image with flasks send_file without saving to disk?

876 views Asked by At

I'm saving the PIL image to a io.BytesIO() object.

imgByteArr = io.BytesIO()
img.save(imgByteArr, format=format)

Then trying to return the image to the user.

return send_file(img.getvalue(), mimetype="image/" + img_details["ext"].lower())

But I'm getting the error

TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

I dont want to send as an attachment, i want the image to be displayed on the page.

Does any one know if it is possible without saving to disk first?

1

There are 1 answers

0
Lewis Morris On BEST ANSWER

I was missing "seek"

imgByteArr = io.BytesIO()
img.save(imgByteArr, format=format)
imgByteArr.seek(0)

return send_file(imgByteArr, mimetype="image/" + img_details["ext"].lower())

This now works.