Home > other >  Can an img tag render a PIL object?
Can an img tag render a PIL object?

Time:08-12

I am using Flask to obtain an image using request.files.get and then manipulating it with PIL. Can I render this PIL object (as a variable in the render function) with the template's img tag's src attribute or will it return an error? In this case, what can I do to make it compatible?

CodePudding user response:

You can either

  1. save the PIL image to a file somewhere that is served by your web server and let the src attribute of the img element point to that file, or

  2. embed the image data into the src value as a data: URL (though I must confess I don’t know how well this works in practice, especially not with anything larger than thumbnail-sized images), or

  3. let the src URL point to a URL which is handled by Flask and where Flask returns image data, just as it normally returns HTML text.

CodePudding user response:

No, the img tag cannot render a PIL object.

  • Related