├── .gitignore ├── LICENSE ├── README.md └── STTinyPNG-Python.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | 55 | # Sphinx documentation 56 | docs/_build/ 57 | 58 | # PyBuilder 59 | target/ 60 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 saitjr 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # STTinyPNG-Python 2 | 3 | TinyPNG 批量压缩图片脚本 4 | 5 | 待解决的问题 : 请保持文件路径中没有中文 (文件名可以是中文) 6 | 7 | ## 一、环境配置 8 | 9 | 别被这四个字吓到了,只需要一行命令。 10 | 11 | 首先,电脑需要有 Python 环境,正好 Mac 自带 Python 环境。然后,安装 TinyPNG 的库: 12 | 13 | ``` ruby 14 | sudo pip install --upgrade tinify 15 | ``` 16 | 17 | ok,环境配置到此结束。 18 | 19 | ## 二、申请 AppKey 20 | 21 | 到[ TinyPNG 网站](https://tinypng.com/developers)上去申请 AppKey,唯一不是很爽的就是一个月只能压缩 500 张(但这不是没限制你可以注册多少个吗…)。填写名字和邮箱,验证以后就可以获得 AppKey 了,很方便。 22 | 23 | ## 三、下载并运行脚本 24 | 25 | 打开 `STTinyPNG-Python.py` ,填写你的 AppKey、图片文件夹路径、图片输出文件夹路径(输出文件夹空的就行,如果图片里的文件夹目录不存在,会自动创建)。 26 | 27 | ``` python 28 | tinify.key = "your AppKey" # AppKey 29 | fromFilePath = "/Users/tangjr/Desktop/test1" # 源路径 30 | toFilePath = "/Users/tangjr/Desktop/test2" # 输出路径 31 | ``` 32 | 33 | ok,运行脚本。打开终端: 34 | 35 | ``` ruby 36 | python /Users/tangjr/Documents/STTinyPNG-Python/STTinyPNG-Python.py 37 | ``` 38 | 39 | Done ! 40 | 41 | ## 四、简单说一下思路 42 | 43 | 使用 Python 的 `os` 库,遍历源文件夹,找到 `.png` 或 `.jpg` 文件,则进行压缩( TinyPNG 只支持这两种图片类型)。 44 | 45 | 恩,就这么简单,没了... 46 | 47 | -------------------------------------------------------------------------------- /STTinyPNG-Python.py: -------------------------------------------------------------------------------- 1 | import tinify 2 | import os 3 | import os.path 4 | 5 | tinify.key = "your AppKey" # AppKey 6 | fromFilePath = "/Users/tangjr/Desktop/test1" # 源路径 7 | toFilePath = "/Users/tangjr/Desktop/test2" # 输出路径 8 | 9 | for root, dirs, files in os.walk(fromFilePath): 10 | for name in files: 11 | fileName, fileSuffix = os.path.splitext(name) 12 | if fileSuffix == '.png' or fileSuffix == '.jpg': 13 | toFullPath = toFilePath + root[len(fromFilePath):] 14 | toFullName = toFullPath + '/' + name 15 | 16 | if os.path.isdir(toFullPath): 17 | pass 18 | else: 19 | os.mkdir(toFullPath) 20 | 21 | with open(toFullName, 'rb') as source: 22 | source_data = source.read() 23 | result_data = tinify.from_buffer(source_data).to_buffer() 24 | --------------------------------------------------------------------------------