I had a similar problem, so I decided to optimize images using OS tools (jpegoptim, optipng, etc.) called from django after saving the model using signals (you can also override the save method). These tools optimize and eliminate metadata from your images. On the other hand, you can study the average compression ratio and size for jpg files 150x150 and try to guess the best quality for customization. Check this out: ( jpeg compression ratio )
This is my code for optimizing files after saving them. I use a convenient thumbnail that provides me with signals after saving:
@receiver(saved_file) def optimize_file(sender, fieldfile, **kwargs): optimize(fieldfile.path) # thumbnail optimization @receiver(thumbnail_created) def optimize_thumbnail(sender, **kwargs): optimize(sender.path) def optimize(path): """ install image utilities apt-get install jpegoptim optipng pngcrush advancecomp :param path: :return: """ # taken from trimage (http://trimage.org/) runString = { ".jpeg": u"jpegoptim -f --strip-all '%(file)s' ; chmod 644 '%(file)s'", ".jpg": u"jpegoptim -f --strip-all '%(file)s' ; chmod 644 '%(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' ; chmod 644 '%(file)s'" } ext = splitext(path)[1].lower() if ext in runString: subprocess.Popen(runString[ext] % {'file': path}, shell=True)
source share