How to display PIL Image dynamically

Juha M

I want to crop my image file using PIL library and display it using Flask and Jinja.

I have tried this code:

@bp.route('/media/<fname>')
def fetch_media(fname):
...
    image = Image.open(path)
    cropped_image = image.crop(box)
    return cropped_image

This gives a TypeError:

The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a Image.

How can I return the Image to html page?

Mark Setchell

Untested, but certainly pretty close to this:

import io
from PIL import Image
from flask import Response

....
....
buffer = io.BytesIO()
cropped_image.save(buffer, format="PNG")
return Response(buffer.getvalue(), mimetype='image/png')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to display PIL image with pygame?

How to dynamically display an image in VueJS?

How to display Image dynamically on UI?

How to response with PIL image in Cherrypy dynamically (Python3)?

Display PIL Image on Kivy Canvas

how to display image in file upload dynamically

how Html helper tag to display dynamically image

How to display image using Image control inside a table dynamically?

Image does not displayed - How to display an image perhaps dynamically

Using PIL or pillow to draw an image and display it with TKinter

holoviews doesn't display PIL image format

Python: How to dynamically display image in label widget (tkinter)

How to display dynamically fetched image on client end in react?

How to get value of input type = file and display it on a dynamically created image?

How to Dynamically display the image source and title for bootstrap cards?

How to display a saved,dynamically created image in django template?

How can I display PIL image to html with render_template flask?

How to get the format of image with PIL?

How to convert a PIL ImageDraw to Image

Dynamically display an image url with javascript

PIL and opencv display different image background from same image

PIL Image.open display image reversely rotated

How to display Arrays Dynamically

How to display an array dynamically

How to display dynamically in Pygame?

Can matshow display an image as imshow with PIL or another library?

Can I display image in full screen mode with PIL?

How to set coordinates when cropping an image with PIL?

How to convert a four dimensional Tensor to image by PIL?