sadegh
sadegh

Reputation: 77

how to prevent from resize the image with every save object in dadtabase?

I have this model:

class Book(models.Model):
   title = models.CharField(max_length=256)
   price = models.IntegerField()
   category = models.ForeignKey('Category', on_delete=models.PROTECT)
   rating = models.FloatField(validators=[MaxValueValidator(10), MinValueValidator(1)])
   discount = models.IntegerField(blank=True, default=0)
   final_price = models.IntegerField(blank=True)
   created_date = models.DateTimeField(auto_now_add=True)
   description = models.TextField(blank=True, null=True)
   count = models.IntegerField(default=0)
   author = models.ForeignKey('Author', on_delete=models.PROTECT)
   image = models.ImageField(upload_to=_get_product_cover_upload_path, null=True, blank=True)

   def resize_image(self):
       img = Image.open(self.image.path)
       img.thumbnail((300, 300))
       img_w, img_h = img.size
       background = Image.new('RGBA', (400, 400), (255, 255, 255, 255))
       bg_w, bg_h = background.size
       offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2)
       background.paste(img, offset)
       background.save(self.image.path)

   def __str__(self):
       return f'{self.title}'

   def save(self, *args, **kwargs):
       self.final_price = self.price * (100 - self.discount) / 100

       super().save(*args, **kwargs)
       self.resize_image()

it worked well but when I modify the book model for example change the price, image will get smaller than last time and with every save the object it get samller... How can I fix it?

Upvotes: 0

Views: 35

Answers (1)

Thomas De Bonnet
Thomas De Bonnet

Reputation: 116

You should have one field for "original_image" and one field for "thumbnail_image".

When you save you take the original_image, resize it, and save it to the thumbnail_image:

class Book(models.Model):
    title = models.CharField(max_length=256)
    price = models.IntegerField()
    category = models.ForeignKey('Category', on_delete=models.PROTECT)
    rating = models.FloatField(validators=[MaxValueValidator(10), MinValueValidator(1)])
    discount = models.IntegerField(blank=True, default=0)
    final_price = models.IntegerField(blank=True)
    created_date = models.DateTimeField(auto_now_add=True)
    description = models.TextField(blank=True, null=True)
    count = models.IntegerField(default=0)
    author = models.ForeignKey('Author', on_delete=models.PROTECT)
    original_image = models.ImageField(upload_to=_get_product_cover_upload_path, null=True, blank=True)
    thumbnail_image = models.ImageField(upload_to=_get_product_cover_upload_path, null=True, blank=True)

    def resize_image(self):
        img = Image.open(self.original_image.path)
        img.thumbnail((300, 300))
        img_w, img_h = img.size
        background = Image.new('RGBA', (400, 400), (255, 255, 255, 255))
        bg_w, bg_h = background.size
        offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2)
        background.paste(img, offset)
        background.save(self.thumbnail_image.path)

    def __str__(self):
        return f'{self.title}'

    def save(self, *args, **kwargs):
        self.final_price = self.price * (100 - self.discount) / 100

        super().save(*args, **kwargs)
        if not self.thumbnail_image:
            self.resize_image()

Upvotes: 1

Related Questions