您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

在Python中使用pngquant压缩png图片的教程

2024/3/4 0:57:34发布33次查看
说到png图片压缩,可能很多人知道tinypng这个网站。但ps插件要钱(虽然有破解的),developer api要连到他服务器去,不提网络传输速度,key也是有每月限制的。
但是貌似tinypng是使用了来自于 pngquant 的技术,至少在 http://pngquant.org/ 中是如此声称的:tinypng and kraken.io — on-line interfaces for pngquant。如果真是这样,我很想对tinypng说呵呵。后者是开源的,连首页中提供的gui工具也都是开源的。并且tinypng在首页的原理说明里面,一次都没提到pngquant
    我取了tinypng的首页上的示例图用pngquant命令行跑了一下,压缩率和显示效果差不多。
    pngquant首页上提供的工具中,pngyu(http://nukesaq88.github.io/pngyu/)是跨平台并且开源的,个人觉得已经相当好用了,直接把文件夹往里面拽就能递归处理,支持各种形式的生成方式(改名、覆盖、存储到其他目录等),压缩结束给出压缩比,并且还支持预览。
    但我还是会希望能够通过脚本来处理,一方面可定制性更强,一方面更方便整合到整个自动化的流程链中。于是我又拿出了python试图写点什么,谁知道……
    pngquant的命令行方式略坑……help中的参数说明和实际效果不一致,已经发现的问题有
    1. --force 参数无效,只要输出文件存在,就会报错,无视这个本用来指定覆写的参数
    2. --skip-if-larger 参数不正常,有时候生成文件明明比较小,也会被skip掉……
    不过好在python大法好,这些问题虽然命令行本身不能处理,但python可以在上层处理掉,下面就是目前实际使用的递归处理某文件夹png的脚本:
'''pngquant.pyuse pngquant to reduces png file sizeruoqian, chen ----------2015/4/31. 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/21. desdir can be the same to srcdir, or another dir2. lod ini config can be not exist----------2015/3/31create'''import osimport os.pathimport sysimport configparserimport stringpngquantexe=pngquantthisfilepath = sys.path[0];print this py file in dir : + thisfilepathprojectpath = thisfilepath + /../cmwar_2dx/cmwar_2dx/;srcresdir = resources/;dstresdir = resources/;lodinipath = projectpath + srcresdir + ini/pic.inikeeporgpaths = [];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 keeporgpathssrcrespath = projectpath + srcresdir;pngcount = 0;transcount = 0;#pngquant --force --skip-if-larger --ext .png --quality 50-90 --speed 1for 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)
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product