forked from saitjr/STTinyPNG-Python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSTTinyPNG-Python.py
More file actions
37 lines (28 loc) · 979 Bytes
/
STTinyPNG-Python.py
File metadata and controls
37 lines (28 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#-*- encoding: UTF-8 -*-
import tinify
import os
import os.path
#tiny官网注册的key
tinify.key = "saO2EtqhoHCfVlbYxHvf-NITmTNX2sXr"
#文件源路径
fromFilePath = "/Users/yclxiao/Project/python/STTinyPNG-Python/big"
#文件目标路径
toFilePath = "/Users/yclxiao/Project/python/STTinyPNG-Python/bignew"
#遍历每一层路径 root是指遍历的文件目录
for root, dirs, files in os.walk(fromFilePath):
for name in files:
fileName, fileSuffix = os.path.splitext(name)
if fileSuffix == '.png' or fileSuffix == '.jpg':
print root
toFullPath = toFilePath + root[len(fromFilePath):]
toFullName = toFullPath + '/' + name
fromFullPath = fromFilePath + root[len(fromFilePath):]
fromFullName = fromFullPath + '/' + name
#如果目录不存在则创建
if os.path.exists(toFullPath):
pass
else:
os.mkdir(toFullPath)
#利用tiny的的api进行图片压缩
source = tinify.from_file(fromFullName)
source.to_file(toFullName)