Skip to content Skip to sidebar Skip to footer

Python - Bottle & Pillow - Return Image To Browser

I use Bottle Framework and Pillow, I want to generate an image on the fly and display it to the browers using the endpoint. I have : try: img = Image.open('images/template.png'

Solution 1:

I'm not at a computer to test my code, but you need to return a PNG-encoded image, so you need to tell PIL to write into a BytesIO rather than to disk. Something like:

from io import BytesIO

# Write PIL Image to in-memory PNG
membuf = BytesIO()
img.save(membuf, format="png") 

... you can now send membuf.getvalue() and if you check the first few bytes they will look exactly the same as if you dump any other, regular PNG file from your disk.


Post a Comment for "Python - Bottle & Pillow - Return Image To Browser"