Django cropper - getting additional resized images from cropped image -
i have problem getting additional resized images croped image. using django-cropper 0.1. model cropped image have part of code this:
class cropped(models.model): def __unicode__(self): return u'%s-%sx%s' % (self.original, self.w, self.h) def upload_image(self, filename): return '%s/crop-%s' % (settings.root, filename) def save(self, *args, **kwargs): #force_insert=false, force_update=false, using=none): source = self.original.image.path target = self.upload_image(os.path.basename(source)) image.open(source).crop([ self.x, # left self.y, # top self.x + self.w, # right self.y + self.h # bottom ]).save(django_settings.media_root + os.sep + target) self.image = target super(cropped, self).save(*args, **kwargs)
but want have additional resized images cropped image change code little , looks that:
class cropped(models.model): def __unicode__(self): return u'%s-%sx%s' % (self.original, self.w, self.h) def _get_average_path(self): return _add_average(self.path) average_path = property(_get_average_path) def _get_average_url(self): return _add_average(self.url) average_url = property(_get_average_url) def _get_large_path(self): return _add_large(self.path) large_path = property(_get_large_path) def _get_large_url(self): return _add_large(self.url) large_url = property(_get_large_url) def upload_image(self, filename): return '%s/crop-%s' % (settings.root, filename) def save(self, *args, **kwargs): #force_insert=false, force_update=false, using=none): source = self.original.image.path target = self.upload_image(os.path.basename(source)) img = image.open(source).crop([ self.x, # left self.y, # top self.x + self.w, # right self.y + self.h # bottom ]).save(django_settings.media_root + os.sep + target) self.image = target super(cropped, self).save(*args, **kwargs) img = image.open(self.image.path) img = img.resize((180, 180), image.antialias) img.save(self.large_path, 'jpeg') img = img.resize((104, 104), image.antialias) img.save(self.average_path, 'jpeg')
but still doing basic job. can me , give suggestion of should fix in code?
i answer own question because found solution , maybe someone. maybe best solution, works. first needed add additional field additional, resized image:
average = models.imagefield( blank=true, null=true, verbose_name = _('image'), upload_to = upload_image, editable = false, )
then needed chnge save() method this:
def save(self, *args, **kwargs): #force_insert=false, force_update=false, using=none): source = self.original.image.path target = self.upload_image(os.path.basename(source)) image.open(source).crop([ self.x, # left self.y, # top self.x + self.w, # right self.y + self.h # bottom ]).save(django_settings.media_root + os.sep + target) self.image = target splited_target = target.split("/") avepath = "/average-".join(splited_target) self.average = avepath image.open(self.image).resize((104, 104), image.antialias).save(django_settings.media_root + avepath, 'jpeg') super(cropped, self).save(*args, **kwargs)
and all.
Comments
Post a Comment