When it comes to PNG image compression, many people may know tinypng this site. But PS plug-in want Money (although have cracked), Developer API to connect to his server, not to mention the network transmission speed, key is also a monthly limit.
But it seems that tinypng is using technology from pngquant, at least in http://pngquant.org/: Tinypng and Kraken.io-on-line interfaces for Pngquant. If this is true, I would like to say to Tinypng hehe. The latter is open source, and even the GUI tools provided in the homepage are open source. and tinypng in the principle of the home page, not once mentioned pngquant
I took the tinypng on the first page of the example diagram with the Pngquant command line ran a bit, the compression rate and display effect is similar.
Pngquant the tools available on the home page, Pngyu (http://nukesaq88.github.io/Pngyu/) is cross-platform and open source, personally feel that has been quite useful, directly to the folder to drag inside can be recursive processing, Support for various forms of generation (renaming, overwriting, storing to other directories, etc.), compression at the end of the compression ratio, and also support the preview.
But I would still like to be able to deal with the script, on the one hand more customizable, on the one hand more easily integrated into the entire automated process chain. So I came up with Python trying to write something, who knew ...
pngquant command line way slightly pits ... the parameter descriptions in Help are inconsistent with the actual effect, and the problems that have been identified are
1. The--force parameter is invalid, as long as the output file exists, it will report an error, ignoring this is used to specify the override parameters
2.--skip-if-larger parameter is not normal, sometimes the generated file is relatively small, will also be skip off ...
But the good thing about Python is that the command line itself can't handle it, but Python can handle it on the top, and here's the script that is actually used to recursively process a folder PNG:
'' '
pngquant.py
use pngquant to reduce png file size
Ruoqian, Chen <piao.polar@gmail.com>
----------
2015/4/3
1. del option --quality = 50-90, special pic need skip can config in lod ini
lod ini format:
[PixelFormat]
map_01.png = 0
0 means skip in file
----------
2015/4/2
1. desDir can be the same to srcDir, or another dir
2. lod ini config can be not exist
----------
2015/3/31
create
'' '
import os
import os.path
import sys
import ConfigParser
import string
PngquantExe = "pngquant"
thisFilePath = sys.path [0];
print "this py file in dir:" + thisFilePath
projectPath = thisFilePath + "/../CMWar_2dx/CMWar_2dx/";
srcResDir = "Resources /";
dstResDir = "Resources /";
lodIniPath = projectPath + srcResDir + "ini / pic.ini"
keepOrgPaths = [];
if os.path.exists (lodIniPath):
config = ConfigParser.SafeConfigParser ()
config.read (lodIniPath)
section = "PixelFormat";
options = config.options (section)
for option in options:
value = string.atoi (config.get (section, option))
if not value:
keepOrgPaths.append (option);
print keepOrgPaths
srcResPath = projectPath + srcResDir;
pngCount = 0;
transCount = 0;
#pngquant --force --skip-if-larger --ext .png --quality 50-90 --speed 1
for parent, dirnames, filenames in os.walk (srcResPath):
print "----- process Dir" + parent
dstDir = parent.replace (srcResDir, dstResDir)
if not os.path.exists (dstDir):
os.makedirs (dstDir)
for filename in filenames:
if os.path.splitext (filename) [1] == '.png':
pngCount + = 1;
srcFilePath = os.path.join (parent, filename);
dstFilePath = os.path.join (dstDir, filename);
tmpFilePath = dstFilePath + ".tmp";
if filename in keepOrgPaths:
print "----- keep -----" + filename;
else:
# print "----- process -----" + filename;
# cmd = "\" "+ PngquantExe +" \ "" + "--force --speed = 1 --quality = 50-90 -v" + srcFilePath + "-o" + tmpFilePath;
cmd = "\" "+ PngquantExe +" \ "" + "--force --speed = 1" + srcFilePath + "-o" + tmpFilePath;
# print cmd;
os.system (cmd)
if os.path.exists (tmpFilePath):
sizeNew = os.path.getsize (tmpFilePath);
sizeOld = os.path.getsize (srcFilePath);
if sizeNew <sizeOld:
open (dstFilePath, "wb"). write (open (tmpFilePath, "rb"). read ())
transCount + = 1;
os.remove (tmpFilePath)
if not os.path.exists (dstFilePath):
open (dstFilePath, "wb"). write (open (srcFilePath, "rb"). read ())
print "Done. Trans Pngs:% d /% d"% (transCount, pngCount)