如何在django管理员方面裁剪图像?

时间:2012-04-02 10:06:02

标签: django image crop

我想在django管理站点中添加图像裁剪功能。我不知道我怎么能接近这个来完成它。我使用过django-image-cropping应用程序,但无法将其集成到管理员端。

1 个答案:

答案 0 :(得分:2)

您应该在模型中定义save()方法:

class MyImage(models.Model):
  image = models.ImageField(...)
  image_crop = models.ImageField(blank=True)

  def save():
    super(MyImage, self).save() #will save only image, image_corp will be blank.

    image_path = self.image.path #path to your non croped image

    #now you can load image file and crop it usung PIL and save.

    self.image_crop = 'path/to/cropped/image' #add path to cropped image.
    super(MyImage, self).save() #save data.