Skip to content

Instantly share code, notes, and snippets.

@crespoxiao
Created September 7, 2016 03:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crespoxiao/59d42266d7f7d8bd217c2947dc54755f to your computer and use it in GitHub Desktop.
Save crespoxiao/59d42266d7f7d8bd217c2947dc54755f to your computer and use it in GitHub Desktop.
python for tinypng
import tinify
import os
import os.path
import sys
print "file name:", sys.argv[0]
for i in range(1, len(sys.argv)):
print "para:", i, sys.argv[i]
sourceFilePath = sys.argv[1] # source path
destinationFilePath = sys.argv[2] # output path
tinify.key = "epMDO4tcQUOMdeqYKNzobSS-pK_ps_hs" # you can change the key
for root, dirs, files in os.walk(sourceFilePath):
for name in files:
fileName, fileSuffix = os.path.splitext(name)
if fileSuffix == '.png' or fileSuffix == '.jpg':
toFullPath = destinationFilePath + root[len(sourceFilePath):]
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