Repository: ylovern/GGTinypng Branch: master Commit: 30ad4b9e1b03 Files: 3 Total size: 4.4 KB Directory structure: gitextract_6zifbauh/ ├── .gitignore ├── README.md └── tinypng.py ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] # C extensions *.so # Distribution / packaging .Python env/ build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ *.egg-info/ .installed.cfg *.egg # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *,cover # Translations *.mo *.pot # Django stuff: *.log # Sphinx documentation docs/_build/ # PyBuilder target/ ================================================ FILE: README.md ================================================ # GGTinypng 批量压缩png和jpg图片python脚本 减少图片50%以上的大小,肉眼看不出差别。减量不减质。 图片数量少的话可以在tinypng.com上试一下。看下效果。 已经支持子文件夹里面的图片,会按原始的相对路径存放到输出文件夹内 7.30 新加功能:一条命令压缩整个项目内的图片。 显著降低app的大小 ## 使用方法 可以使用alias来简化命令 python3 tinypng.py -i -o -o 参数可以为空,因为要遍历picDocPath子文件夹 所以默认outputDocPath改为picDocPath父目录 内的outputTinypng文件夹,与picDocPath同级。 7.30 新参数:python3 tinypng.py -r <项目路径或者项目内某个图片文件夹的路径> -r 参数会压缩文件夹内的图片并替换原图片 去 https://tinypng.com/developers 免费申请自己的key 每key每月免费压缩500个图 默认并发数为10 可以自己调整 ##to do 如果有时间的话可能会做成处理mac程序或者xcode插件。不过短时间内不会,因为我自己用的话 脚本就足够了。 ================================================ FILE: tinypng.py ================================================ #!/usr/bin/env python3 # coding=utf-8 # from os.path import dirname import os,sys, getopt from urllib.request import Request, urlopen from base64 import b64encode from multiprocessing import Pool poolLimite = 10 key = "PX-pm9lAY3siS8cHIWz44zWFZHj6TtYX" # input = "large-input.png" # output = "tiny-output.png" opts, args = getopt.getopt(sys.argv[1:], "hi:o:r:") input_doc_path="" output_doc_path = '' filePaths=[] for op, value in opts: if op == "-i": input_doc_path = value elif op == "-o": output_doc_path = value elif op == "-r": input_doc_path = value output_doc_path = value elif op == "-h": print(''' 使用方法 python3 tinypng.py -i picDocPath -o outputDocPath -o 参数可以为空,默认存在picDocPath/tinypng 内 去 https://tinypng.com/developers 申请自己的key 每key每月免费压缩500个图 默认并发数为10 可以自己调整''') def absFilePath(fileName): return os.path.join(input_doc_path,fileName) def getTinyPng(filePath): print('开始'+filePath) request = Request("https://api.tinify.com/shrink", open(filePath, "rb").read()) cafile = None # Uncomment below if you have trouble validating our SSL certificate. # Download cacert.pem from: http://curl.haxx.se/ca/cacert.pem # cafile = dirname(__file__) + "/cacert.pem" auth = b64encode(bytes("api:" + key, "ascii")).decode("ascii") request.add_header("Authorization", "Basic %s" % auth) response = urlopen(request, cafile = cafile) if response.status == 201: # Compression was successful, retrieve output from Location header. result = urlopen(response.getheader("Location"), cafile = cafile).read() output = os.path.join(output_doc_path, os.path.relpath(filePath,input_doc_path)) open(output, "wb").write(result) print('完成'+output) else: print('失败'+filePath) # Something went wrong! You can parse the JSON body for details. print("Compression failed") def main(): global output_doc_path if output_doc_path == '': output_doc_path = os.path.join(os.path.split(input_doc_path)[0], 'outputTinypng') if not os.path.exists(output_doc_path): os.mkdir(output_doc_path) for parent,dirnames,filenames in os.walk(input_doc_path): #三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字 for dirname in dirnames: #输出文件夹信息 # print("parent is:" + parent) # print("dirname is" + dirname) outDir = os.path.join(output_doc_path,os.path.relpath(os.path.join(parent,dirname),input_doc_path)) if not os.path.exists(outDir): os.mkdir(outDir) for filename in filenames: #输出文件信息 # print("parent is:" + parent) # print("filename is:" + filename) filePaths.append(os.path.join(parent,filename)) pngFilePaths = filter(lambda x:os.path.splitext(x)[1]=='.png' or os.path.splitext(x)[1]=='.jpg',filePaths) print('Parent process %s.' % os.getpid()) p = Pool(poolLimite) for fileName in pngFilePaths: p.apply_async(getTinyPng, args=(fileName,)) print('Waiting for all subprocesses done...') p.close() p.join() print('All subprocesses done.') if __name__=='__main__': if os.path.isdir(input_doc_path): main()