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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---