python PNG优化工具

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1993678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:30:33  来源:igfitidea点击:

PNG optimisation tools

phppythonoptimizationpng

提问by Mathew

A while back I used a PNG optimisation service called (I think) "smush it". You fed it a weblink and it returned a zip of all the PNG images with their filesizes nicely, well, smushed...

不久前,我使用了一个名为(我认为)“smush it”的 PNG 优化服务。你给它提供了一个网络链接,它返回了一个包含所有 PNG 图像的压缩文件,它们的文件大小很好,很好,被弄脏了......

I want to implement a similar optimisation feature as part of my website's image upload process; does anyone know of a pre-existing library (PHP or Python preferably) that I can tap into for this? A brief Google has pointed me towards several command line style tools, but I'd rather not go down that route if possible.

我想在我网站的图片上传过程中实施类似的优化功能;有没有人知道我可以为此利用的预先存在的库(最好是 PHP 或 Python)?一个简短的谷歌向我指出了几个命令行风格的工具,但如果可能的话,我宁愿不走这条路。

回答by Burntime

Execute with PHP this command line tools

用PHP执行这个命令行工具

  pngcrush -rem gAMA -rem cHRM -rem iCCP -rem sRGB -brute -l 9 -max -reduce -m 0 -q IMAGE
  optipng -o7 -q pngout.png
  pngout pngout.png -q -y -k0 -s0
  advpng -z -4 pngout.png > /dev/null

回答by Mike

As long as your PHP is compiled with GD2 support (quite common nowadays):

只要你的 PHP 是用 GD2 支持编译的(现在很常见):

<?php
$image = imagecreatefromstring(file_get_contents('/path/to/image.original.png'));
imagepng($image, '/path/to/image.smushed.png', 9);

This will read in any image format GD2 understands (not just PNG) and output a PNG gzipped as the maximum compression level without sacrificing quality.

这将读取 GD2 理解的任何图像格式(不仅仅是 PNG),并在不牺牲质量的情况下输出 gzip 为最大压缩级别的 PNG。

It might be of less use today than years ago though; most image editors already do this, since gzipping doesn't cost as much CPU-wise as it used to.

不过,与几年前相比,它今天的用处可能更少。大多数图像编辑器已经这样做了,因为 gzipping 在 CPU 方面的成本不像以前那么高。

回答by Kache

Have you heard of PNGCrush? You could check out the source, part of PNG and MNG Tools at SourceForge, and transcribe or wrap it in Python.

你听说过PNGCrush吗?您可以在 SourceForge 上查看源代码、PNG 和 MNG 工具的一部分,然后将其转录或包装在 Python 中。

回答by David Jones

I would question the wisdom of throwing away other chunks (like gAMA and iCCP), but if that's what you want to do it's fairly easy to use PyPNGto remove chunks:

我会质疑扔掉其他块(如 gAMA 和 iCCP)的智慧,但如果这是你想做的,使用PyPNG删除块相当容易:

#!/usr/bin/env python
import png
import sys

input=sys.stdin
out=sys.stdout

def critical_chunks(chunks):
    for type,data in chunks:
        if type[0].isupper():
            yield type,data

chunks = png.Reader(file=input).chunks()
png.write_chunks(out, critical_chunks(chunks))

the critical_chunks function is essentially filtering out all but the critical PNG chunks (the 4 letter type for a critical chunk starts with an uppercase letter).

critical_chunks 函数实质上是过滤掉除关键 PNG 块以外的所有块(关键块的 4 字母类型以大写字母开头)。