License MIT license
Lines 17
Keywords
django (3) image (5) ImageField (1) PIL (2)
Permissions
Viewable by Everyone
Editable by All Siafoo Users
Hide
Need a quick chart or graph for your blog? Try our reStructured Text renderer. Join Siafoo Now or Learn More

Make Thumbnails of Django ImageFields Atom Feed 0

In Brief Given an ImageField image, create a thumbnail of it (above at 75x90) and prepare it to be saved to another ImageField.
# 's
 1try:
2 from PIL import Image
3except ImportError:
4 import Image
5from StringIO import StringIO
6
7def make_thumb(image, thumb_field_name):
8 im = Image.open(StringIO(''.join(image.chunks())))
9 thumb = im.resize((75, 90), resample = Image.ANTIALIAS)
10 thumb_io = StringIO()
11 thumb.save(thumb_io, format='JPEG')
12 thumbnail = InMemoryUploadedFile(thumb_io, thumb_field_name, '%s_thumb.jpg' % image.name.rsplit('.', 1)[0], 'image/jpeg', thumb_io.len, None)
13 return thumbnail
14
15# Use:
16# thumbnail = make_thumb(image_field)
17# obj = MyModel(image = image, thumbnail = thumbnail) #etc
18# obj.save()

Given an ImageField image, create a thumbnail of it (above at 75x90) and prepare it to be saved to another ImageField.