On Nov 4, 2:33 am, Chris Amico <[EMAIL PROTECTED]> wrote:
> Hi folks,
>
> I'm trying to turn one uploaded image into several: a full-size and
> thumbnail (and more, eventually). Here's the model I'm playing with,
> just for testing:
>
> class TestPhoto(models.Model):
> "This is only for testing. Delete it later and make a real photo
> model"
> title = models.CharField(max_length=100)
> pub_date = models.DateTimeField(auto_now_add=True)
> full_size = models.ImageField(upload_to="images/photos/%Y/%b/%d")
> thumbnail = models.ImageField(upload_to="images/photos/%Y/%b/%d",
> blank=True)
>
> Resizing is simple enough using a script off Django Snippets:
>
> def thumbnail(filename, size=(50, 50), output_filename=None):
> image = Image.open(filename)
> if image.mode not in ('L', 'RGB'):
> image = image.convert('RGB')
> image = image.resize(size, Image.ANTIALIAS)
>
> # get the thumbnail data in memory.
> if not output_filename:
> output_filename = get_default_thumbnail_filename(filename)
> image.save(output_filename, image.format)
> return output_filename
>
> That part works like a charm when I run it on save(), and the returned
> files end up exactly where they should be. But how do I get Django to
> recognize that output file as a File object?
>
> I've tried adding another ImageField to the model and using a save()
> method to populated it with the output_filename from thumbnail(). It
> gets the path right, but the url ends up being something like
> MEDIA_URL + self.full_size.path + "thumb.jpg" or media.mydomain.com/
> home/chrisamico/webapps/media/images/photos/2008/Oct/23/
> IMG_0460.thumb.jpg. That's a big 404.
>
> I could write a simple method to create the right url (since it's just
> full_size.url with .thumb.jpg on the end) but that would be repetitive
> for each size.
>
> Thanks as always for the help.
I have been overriding the save method on the model. I have found
that by the time the save method is called, the file is saved to the
disk already, So I open the original, resize and save to the
thumbnail:
from __future__ import division
import os
import tempfile
import Image
from django.core.files import File
class Picture(models.Model)
name = models.CharField(max_length=200)
original = models.ImageField(upload_to='original')
thumbnail = models.ImageField(upload_to='thumbnail')
def save(self, force_update=False, force_insert=False):
#Open image as PIL Image and get name
orig = Image.open(self.original.path)
name = os.path.basename(self.original.name)
#using future division, calculating height for width of
100 to keep aspect
height = int(100 * orig.size[1] / orig.size[0])
#Resize, open tempfile, and save to temp
thumb = orig.resize((width, height), Image.ANTIALIAS)
thumb_file = tempfile.NamedTemporaryFile('w+b')
thumb.save(thumb_file, 'JPEG')
#Save tempfile to thumbnail
self.thumbnail.save(name, File(thumb_file), False)
thumb_file.close() #tempfile is deleted upon close:)
super(Picture, self).save(force_update, force_insert)
I hope this is clear enough. I actually have more in this for
specifying the size of the thumbnail and some other processing, but
this is the core of how I do it.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---