1

So, I'm trying to do a little bit of compression of images as well as some other operations. I had a few questions...I have the following save() method for my user class:

class User(AbstractBaseUser, PermissionsMixin):
    ...
    avatar = models.ImageField(storage=SITE_UPLOAD_LOC, null=True, blank=True)

    def save(self, *args, **kwargs):
        if self.avatar:
            img = Img.open(BytesIO(self.avatar.read()))

            if img.mode != 'RGB':
                img = img.convert('RGB')

            new_width = 200
            img.thumbnail((new_width, new_width * self.avatar.height / self.avatar.width), Img.ANTIALIAS)
            output = BytesIO()
            img.save(output, format='JPEG', quality=70)
            output.seek(0)
            self.avatar= InMemoryUploadedFile(file=output, field_name='ImageField', name="%s.jpg" % self.avatar.name.split('.')[0], content_type='image/jpeg', size=, charset=None)

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

I had two questions:

  1. Best way for deleting the old avatar file on save, if a previous avatar exists
  2. What do I pass into InMemoryUploadedFile for the size kwarg? Is this the size of the file? in what unit/(s)?
Denis
  • 7,127
  • 8
  • 37
  • 58
GCien
  • 2,221
  • 6
  • 30
  • 56
  • The first question is a duplicate of this: https://stackoverflow.com/questions/2878490/how-to-delete-old-image-when-update-imagefield/12058684 Ask only one question at a time. If you have two questions, post them separately. – Håken Lid Feb 16 '18 at 09:06
  • Possible duplicate of [How to delete old image when update ImageField?](https://stackoverflow.com/questions/2878490/how-to-delete-old-image-when-update-imagefield) – Håken Lid Feb 16 '18 at 09:06

2 Answers2

1

You need to get file size. Try this:

import os

size = os.fstat(output.fileno()).st_size

You can read this for how to get file size: https://docs.python.org/3.0/library/stat.html

and for deleting old avtar. According to your code it is foreign key hence before saving you can check if avtar is already exists and so yoy can delete it. Add this lines code after output.seek(0) this line:

if self.avtar:
    self.avtar.delete()
Diksha
  • 384
  • 1
  • 5
  • 16
1

What you want is the size in bytes.

You can get the byte size of a BytesIO object like this.

size = len(output.getvalue())
Håken Lid
  • 22,318
  • 9
  • 52
  • 67