Uh oh!
There was an error while loading. Please reload this page.
forked from hexi/python-imageTools
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCompressImage.py
More file actions
Latest commit
107 lines (83 loc) · 3.32 KB
/
Copy pathCompressImage.py
File metadata and controls
107 lines (83 loc) · 3.32 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
#-*- encoding:utf-8 -*-
importos
# /usr/local/lib/python3.4/site-packages/PIL
fromPILimportImage
importsys
fromListImageimportListImage
importtinify
tinify.key='tinify.key'#请改为自己的tinify key
sizeShouldCompress=1*1024
defis_un_compressed(image):
f9patch='.9.'
ifimage.find(f9patch) >0:
returnFalse
withImage.open(image) asim:
returnim.format=='PNG'andim.mode!='L'andim.mode!='P'and (os.stat(image).st_size>=sizeShouldCompress)
defis_not_png(image):
withImage.open(image) asim:
returnim.format!='PNG'
defcompress_image(image):
print('compressing image:', image)
tinify.from_file(image).to_file(image)
defbatch_compress(images):
print('------------------------start to compress image----------------------')
forimageinimages:
compress_image(image)
print('------------------------end to compress image----------------------')
if__name__=='__main__':
baseDir=os.path.abspath('.')
shouldCompress=True
shouldListImags=False
defpaseBaseDirParm(args):
iflen(args) >=2:
baseDir=args[1]
print("from input baseDir: %s"%baseDir)
returnbaseDir
else:
returnos.path.abspath('.');
defparseSizeShouldCompressPram(args):
iflen(args) >=3:
in_size=int(args[2])
sizeShouldCompress=in_size*1024
print("from input sizeShouldCompress: %d"%in_size)
returnsizeShouldCompress
else:
return1*1024
defparseShouldCompressParam(args):
iflen(args) >=4:
try:
shouldCompress=bool(int(args[3]))
print("from input shouldCompress: %s"%shouldCompress)
returnshouldCompress
exceptExceptionase:
print('!!!! the third parameter must be 0 or 1')
else:
returnTrue
defpaseShouldListImagesParam(args):
iflen(args) ==5:
try:
shouldListImags=bool(int(args[4]))
print("from input shouldListImags: %s"%shouldListImags)
returnshouldListImags
exceptExceptionase:
print('!!!! the forth parameter must be 0 or 1')
else:
returnFalse
baseDir=paseBaseDirParm(sys.argv)
sizeShouldCompress=parseSizeShouldCompressPram(sys.argv)
shouldCompress=parseShouldCompressParam(sys.argv)
shouldListImags=paseShouldListImagesParam(sys.argv)
print('===params, baseDir: %s, sizeShouldCompress:%d, shouldCompress:%s, shouldListImags:%s'% (baseDir, sizeShouldCompress/1024, shouldCompress, shouldListImags))
listImage=ListImage(baseDir)
imageFiles=listImage.images()
uncompressed=list(filter(is_un_compressed, imageFiles))
unPngFiles=list(filter(is_not_png, imageFiles))
ifshouldListImags:
print('===searched %d images======>\n%s'% (len(imageFiles), imageFiles))
print('===uncompressed png %d files --------------------->'%len(uncompressed))
print(uncompressed)
print('===jpg or webp %d files ------------------------->'%len(unPngFiles))
print(unPngFiles)
ifshouldCompress:
batch_compress(uncompressed)