Hello,
i want to compress images and by myself i couldn’t do it.i found this solution, that works.the thing I don’t understand are, where the file is saved, and the file after the process is finish, automatically deleted or remains somewhere,I would be grateful if anyone can explain to me how InMemoryUploadedFile works
class Image(models.Model):
owner= models.ForeignKey(settings.AUTH_USER_MODEL)
title = models.CharField(max_length=255)
image = models.ImageField(upload_to=get_uplaod_file_name)
def save(self):
im = Image.open(self.image)
output = BytesIO()
im = im.resize((100, 100))
im.save(output, format='JPEG', quality=90)
output.seek(0)
self.image = InMemoryUploadedFile(output, 'ImageField', "%s.jpg" % self.image.name.split('.')[0], 'image/jpeg',
sys.getsizeof(output), None)
super(Image, self).save()