Created
January 22, 2016 07:10
-
-
Save paranoidjk/a78fed7384ef795eb34e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tinify | |
| import os | |
| import os.path | |
| tinify.key = "your AppKey" # AppKey | |
| fromFilePath = "/Users/tangjr/Desktop/test1" # 源路径 | |
| toFilePath = "/Users/tangjr/Desktop/test2" # 输出路径 | |
| for root, dirs, files in os.walk(fromFilePath): | |
| for name in files: | |
| fileName, fileSuffix = os.path.splitext(name) | |
| if fileSuffix == '.png' or fileSuffix == '.jpg': | |
| toFullPath = toFilePath + root[len(fromFilePath):] | |
| toFullName = toFullPath + '/' + name | |
| if os.path.isdir(toFullPath): | |
| pass | |
| else: | |
| os.mkdir(toFullPath) | |
| source = tinify.from_file(root + '/' + name) | |
| source.to_file(toFullName) | |
| with open(toFullName, 'rb') as source: | |
| source_data = source.read() | |
| result_data = tinify.from_buffer(source_data).to_buffer() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment