#acl All:read,write [[Django1.0]]에서 이미지 썸네일 만들기 [[http://code.djangoproject.com/wiki/ThumbNails|썸네일 만드는 다양한 방법들]]을 참고할 수 있는데, 그 가운데 [[http://superjared.com/entry/django-quick-tips-2-image-thumbnails/|원본저장시 하나의 썸네일을 동시저장]]하는 방법이 간편한 방법이라 할만하다. 하지만, 1.0에서는 동작하지 아니함. 1.0을 위해서는 아래와 같아야 함. {{{#!python from django.db import models class Photo(models.Model): """ A photo for my site """ title = models.CharField(maxlength=50) photo = models.ImageField(upload_to="photos/") thumbnail = models.ImageField(upload_to="thumbnails/", editable=False) def save(self): from PIL import Image from cStringIO import StringIO from django.core.files.uploadedfile import SimpleUploadedFile # Set our max thumbnail size in a tuple (max width, max height) THUMBNAIL_SIZE = (50, 50) # Open original photo which we want to thumbnail using PIL's Image # object # image = Image.open(self.photo.name) # as of Django 1.2 at least, .name doesn't work. image = Image.open(self.photo) # Convert to RGB if necessary # Thanks to Limodou on DjangoSnippets.org # http://www.djangosnippets.org/snippets/20/ if image.mode not in ('L', 'RGB'): image = image.convert('RGB') # We use our PIL Image object to create the thumbnail, which already # has a thumbnail() convenience method that contrains proportions. # Additionally, we use Image.ANTIALIAS to make the image look better. # Without antialiasing the image pattern artifacts may result. image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS) # Save the thumbnail temp_handle = StringIO() image.save(temp_handle, 'png') temp_handle.seek(0) # Save to the thumbnail field suf = SimpleUploadedFile(os.path.split(self.photo.name)[-1], temp_handle.read(), content_type='image/png') self.thumbnail.save(suf.name+'.png', suf, save=False) # Save this photo instance super(Photo, self).save() class Admin: pass def __str__(self): return self.title }}}