我想处理通过简单缩略图通过PIL通过普普提生成的所有JPEG缩略图。
使用PIL的优化:image.save(..,optimize=1,...)一点也不优化。
例如:
有人能告诉我集成jpegoptim的Python示例或库吗?
发布于 2012-03-22 21:35:39
我发现了https://github.com/thebeansgroup/smush.py,它是Python >=2.7中的一个无损图像优化器。我使用了https://github.com/beatak/smush.py,因为我们在服务器上使用的是>=稳定,这是一个用于PythonDebian2.5的叉子。
它使用:
希望在文件上多次使用pngnq不会降低质量,我们计划每周在所有上传的媒体上运行这个脚本。
发布于 2012-09-25 02:41:44
您可以使用thumbnail_created信号并通过subporecess.Popen调用外部应用程序。我只是在我的项目中意识到了这一点。您甚至可以优化图片时,他们上传使用saved_file信号!
这是我的代码:
import subprocess
from os.path import splitext
from django.dispatch import receiver
from easy_thumbnails.signals import saved_file, thumbnail_created
@receiver(saved_file)
def optimize_file(sender, fieldfile, **kwargs):
optimize(fieldfile.path)
@receiver(thumbnail_created)
def optimize_thumbnail(sender, **kwargs):
optimize(sender.path)
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:
subprocess.Popen(runString[ext] % {'file': path}, shell=True)runString是从修边拿走的。在Debian上,您需要安装以下软件包:jpegoptim optipng pngcrush advancecomp。或者只需使用其他工具,如smush.py。
我还发现了这个项目,它封装了上面的代码,具有gif支持和更好的文件类型识别。
发布于 2012-03-19 23:24:32
我怀疑是否有到jpegoptim的python绑定。我能想到的选择是:
https://stackoverflow.com/questions/9779052
复制相似问题