I have a website developed with Django 1.10. The users can send images to the website. Unfortunately there are images very heavy ! ( > 1 mo).
Currently when the dimension is greater or equal to 4000*4000, I resize to 128*128 but it's not sufficient for the weight...
Google advises to use Jpegoptim and Optipng. So I tried to use these tools directly in my Django code but it doesn't works... (I already install jpegoptim and optipng) :
def optimize(path):
runString = {
".jpeg": u"jpegoptim -f --strip-all '%(file)s'",
".jpg": u"jpegoptim -f --strip-all '%(file)s'",
".png": u"optipng -force -o7 '%(file)s' && advpng -z4 '%(file)s' && pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP -rem sRGB -rem time '%(file)s' '%(file)s.bak' && mv '%(file)s.bak' '%(file)s'"
}
ext = splitext(path)[1].lower()
if ext in runString :
print("ok")
subprocess.Popen(runString[ext] % {'file': path}, shell=True)
class ImageUser(models.Model):
image = models.ImageField(upload_to="imageUser/", null=True)
def save(self, *args, **kwargs) :
super(ImageUser, self).save()
image = Image.open(self.image.path)
optimize(self.image.path)
image.save(self.image.path)
I want to reduce the weight of the images before the saving if it's possible. Where I am wrong ? Thank you