Nic James Ferrier escribió: > Chris Hoeppner <[EMAIL PROTECTED]> writes: > >> Maybe something like >> >> def save(self): >> self.imageField = pil.thumbnail(self.imageField) >> >> Can't really tell if I'm on the right track here. > > Search the list... I've got this from the list: > > > IMAGE_SIZE = [300, 300] > THUMBNAIL_SIZE = [100, 100] > UPLOAD_TO="photos/%Y/%M%d" > THUMBS_TO="thumbs/%Y/%M%d" > > class Mugshot(models.Model): > """Represents a single picture of the user""" > > person = models.ForeignKey(Person) > shot = models.ImageField(upload_to=UPLOAD_TO) > thumb = models.ImageField(upload_to=THUMBS_TO, editable=False) > name = models.CharField(maxlength=500, blank=True) > > def __str__(self): > return self.name > > def get_url(self): > try: > m = re.match(settings.MEDIA_ROOT + "(.*)", > self.get_shot_filename()) > return n.group(1) > except: > return self.get_shot_filename() > > > # Django thumbs hack from super jared. > # http://superjared.com/entry/django-quick-tips-2-image-thumbnails/ > def save(self): > logger = logging.getLogger("Mugshot.save") > from PIL import Image > if not self.thumb: > self.save_thumb_file(self.get_shot_filename(), '') > image = Image.open(self.get_shot_filename()) > # 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') > > cropped = utils.image_crop(image, IMAGE_SIZE) > cropped.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS) > cropped.save(self.get_thumb_filename()) > > # Save this photo instance > super(Mugshot, self).save() > > # Now resize the main image > image = Image.open(self.get_shot_filename()) > if image.mode not in ('L', 'RGB'): > image = image.convert('RGB') > > cropped = utils.image_crop(image, IMAGE_SIZE) > cropped.thumbnail(IMAGE_SIZE, Image.ANTIALIAS) > cropped.save(self.get_shot_filename()) > > class Admin: > pass >
If I get this right, you're saving the original file, and a separate thumb file. While this seems interesting, it's not what I want to implement. It's more of a bit of catching the incoming data and transforming it before it gets saved in the model instance. On djangosnippets I've seen a thumbnail function able to work on provided data rather than a provided file path. This might be just what I need :) But since, I'm very new, I'm having hard times when something doesn't work. Debugging is an art of it's own :)
signature.asc
Description: OpenPGP digital signature