Login

Image model with thumbnail

Author:
clawlor
Posted:
July 4, 2010
Language:
Python
Version:
Not specified
Tags:
image thumbnails thumbs
Score:
1 (after 1 ratings)

A relatively simple Photo model which generates its own thumbnail when saved. A default size is specified as thumb_size in the save() arguments.

Other thumbnailing strategies don't save the thumbnail dimensions, and since the actual dimensions of the thumbnail created by PIL's thumbnail method are somewhat non-deterministic, it is difficult to create an img tag with proper height and width attributes. This approach makes that task simple.

This approach works well if only one thumbnail size is required. It could easily be adapted to support two or three thumbnail sizes, but adding more sizes would quickly get unwieldy.

This was adapted from http://biohackers.net/wiki/Django1.0/Thumbnail

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from PIL import Image
from cStringIO import StringIO
from django.core.files.uploadedfile import SimpleUploadedFile

class Photo(models.Model):
    title = models.CharField(max_length = 100)
    image = models.ImageField(upload_to ="photos/originals/%Y/%m/")
    image_height = models.IntegerField()
    image_width = models.IntegerField()
    thumbnail = models.ImageField(upload_to="photos/thumbs/%Y/%m/")
    thumbnail_height = models.IntegerField()
    thumbnail_width = models.IntegerField()
    caption = models.CharField(max_length = 250, blank =True)
    
    def __str__(self):
        return "%s"%self.title
    
    def __unicode__(self):
        return self.title
        
    def save(self, force_update=False, force_insert=False, thumb_size=(180,300)):

        image = Image.open(self.image)
        
        if image.mode not in ('L', 'RGB'):
            image = image.convert('RGB')
            
        # save the original size
        self.image_width, self.image_height = image.size
        
        image.thumbnail(thumb_size, Image.ANTIALIAS)
        
        # save the thumbnail to memory
        temp_handle = StringIO()
        image.save(temp_handle, 'png')
        temp_handle.seek(0) # rewind the file
        
        # save to the thumbnail field
        suf = SimpleUploadedFile(os.path.split(self.image.name)[-1],
                                 temp_handle.read(),
                                 content_type='image/png')
        self.thumbnail.save(suf.name+'.png', suf, save=False)
        self.thumbnail_width, self.thumbnail_height = image.size
        
        # save the image object
        super(Photo, self).save(force_update, force_insert)

More like this

  1. Image Standarization by garcia_marc 6 years, 10 months ago
  2. ThumbnailMixIn by johan 5 years, 10 months ago
  3. Generate thumbnails on save by peterw 6 years, 3 months ago
  4. Resize image on save by David 6 years, 10 months ago
  5. Function to create resized versions of an image from a URL and saving it to a local path by obeattie 8 years ago

Comments

Please login first before commenting.