Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display a PIL Image object in a template?

If a user uploads an image, and I resize it using PIL, I get a PIL Image object.

How do I display a PIL Image file in a template, before it has been saved to the database? Can it even be passed in as an image and rendered?

like image 779
sgarza62 Avatar asked Jan 15 '13 22:01

sgarza62


People also ask

How display PIL image in HTML?

You can embed base64 encoded images into an tag. So you could convert PIL image to base64 and then display it.

How do I display PIL images?

Python – Display Image using PIL To show or display an image in Python Pillow, you can use show() method on an image object. The show() method writes the image to a temporary file and then triggers the default program to display that image.

How do I import photos from PIL?

To load the image, we simply import the image module from the pillow and call the Image. open(), passing the image filename. Instead of calling the Pillow module, we will call the PIL module as to make it backward compatible with an older module called Python Imaging Library (PIL).

What is PIL image object?

PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL. Image. new() method creates a new image with the given mode and size. Size is given as a (width, height)-tuple, in pixels.


3 Answers

For a limited set of browsers, you can base64 encode the image and use inline images. See Embedding Base64 Images.

A solution that works for all browsers is an image tag referencing a view that returns the image.

[update]

All I want is for the user to submit the original image, and then be prompted by another form to input a caption for the image (with the resized image to the left of the caption field). Then when the user hits "submit" the image and the caption get saved in a model instance.

Well... When you use <img src="foo">, foo is always retrieved by a GET perhaps that is why it is not working - request.FILES will not be available in a GET request. If you open firebug or the chrome debug toolbar, in the network tab, you will see the POST request with the uploaded image and after that a GET request to fetch the image.

You have to save the image somewhere between both steps.

how else could i save it? I would love for it to be temporary. Do you think there's a really easy way to do this, or should I go look into those options?

Popular choices are redis and memcached. You can think of them as giant shared python dict with an expire date. If the images are small, like an avatar, you can also save the image data in a session variable.

like image 187
Paulo Scardine Avatar answered Oct 09 '22 17:10

Paulo Scardine


Yes and no.

Yes, you can put the images as raw Base64 data. Here's a little script you can use to test this:

import Image
import base64
import StringIO
output = StringIO.StringIO()
im = Image.open("test.png") # Your image here!
im.save(output, format='PNG')
output.seek(0)
output_s = output.read()
b64 = base64.b64encode(output_s)
open("test.html","w+").write('<img src="data:image/png;base64,{0}"/>'.format(b64))

However, this is a really bad idea. With multiple thumbnails, your single HTML page might be 10MB+.

What you really should be doing is using a separate Django view to return images from PIL objects as PNG files, and then referencing that view in the img href attributes on your page.

like image 16
Fredrick Brennan Avatar answered Oct 09 '22 17:10

Fredrick Brennan


You can embed base64 encoded images into an tag. So you could convert PIL image to base64 and then display it.

from PIL import Image
import StringIO

x = Image.new('RGB',(400,400))
output = StringIO.StringIO()
x.save(output, "PNG")
contents = output.getvalue().encode("base64")
output.close()
contents = contents.split('\n')[0]

Then show with:

 <img src="data:image/png;base64,' + contents + ' />

Look to an example output.

like image 8
Ahmet Aygun Avatar answered Oct 09 '22 17:10

Ahmet Aygun