Skip to content

Instantly share code, notes, and snippets.

@paranoidjk
Created January 22, 2016 07:10
Show Gist options
  • Select an option

  • Save paranoidjk/a78fed7384ef795eb34e to your computer and use it in GitHub Desktop.

Select an option

Save paranoidjk/a78fed7384ef795eb34e to your computer and use it in GitHub Desktop.
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