-
Notifications
You must be signed in to change notification settings - Fork 5
/
tinypng.py
195 lines (132 loc) · 6.52 KB
/
tinypng.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# -*- coding: utf-8 -*-
# Python3
import os
import sys
import warnings
#import subprocess as sp
import numpy as np
from PIL import Image
from sklearn.cluster import MiniBatchKMeans
OPTIPNG_EXECUTABLE_FILE_NAME_LINUX = 'optipng'
OPTIPNG_EXECUTABLE_FILE_NAME_WINDOWS = 'optipng.exe'
def loadImageFileAsRGBArray(fname) :
img = Image.open(fname)
with warnings.catch_warnings():
warnings.filterwarnings('ignore')
img_rgb = img.convert('RGB')
img.close()
return np.asarray(img_rgb)
def saveImageAsPalettePNGusingKMeans(quant, img_array2d, fname) :
h, w, _ = img_array2d.shape
kmeans_n_clusters = min(quant, w*h)
img_array1d = img_array2d.reshape((-1,3))
with warnings.catch_warnings():
warnings.filterwarnings('ignore')
kmeans_res = MiniBatchKMeans(n_clusters=kmeans_n_clusters, init='k-means++', max_iter=100, batch_size=32768, compute_labels=True, random_state=0).fit(img_array1d)
palette_array = np.array(kmeans_res.cluster_centers_ + 0.5, dtype=np.uint8)
img_palette_array1d = palette_array[kmeans_res.labels_]
img_palette_array2d = img_palette_array1d.reshape(img_array2d.shape)
Image.fromarray(img_palette_array2d).convert('P', palette=Image.ADAPTIVE).save(fname, optimize=True)
return kmeans_res.n_iter_
def callOptiPNG (optipng_path, dst_fname) :
#COMMANDS = [ optipng_path, '-o7', dst_fname ]
#p = sp.Popen(COMMANDS, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
#p.wait()
command = optipng_path + ' -o7 ' + dst_fname
print(command)
os.system(command)
def copy_file (src_path, dst_path) :
if os.path.isfile(src_path) and not os.path.exists(dst_path) :
with open(src_path, 'rb') as src_fp :
with open(dst_path, 'wb') as dst_fp :
dst_fp.write(src_fp.read())
if __name__ == '__main__':
# judge OS --------------------------------------------------------
if sys.platform.lower().startswith('win'):
print('Windows')
optipng_executable_fname = OPTIPNG_EXECUTABLE_FILE_NAME_WINDOWS
elif sys.platform.lower().startswith('linux'):
print('Linux')
optipng_executable_fname = OPTIPNG_EXECUTABLE_FILE_NAME_LINUX
else:
print('current OS %s not support' % sys.platform)
exit(-1)
# get the path of optipng --------------------------------------------------------
work_path = sys.argv[0]
work_dir, _ = os.path.split(work_path)
if work_dir == '' :
optipng_path = '.' + os.path.sep + optipng_executable_fname
else :
optipng_path = work_dir + os.path.sep + optipng_executable_fname
# parse command args --------------------------------------------------------
try :
src_dir = sys.argv[1]
dst_dir = sys.argv[2]
if len(sys.argv) > 3 :
quant = int(sys.argv[3])
else :
quant = 256
except:
print('Usage: python %s <source_directory> <destination_directory> [<quant(2~256)>]' % sys.argv[0])
exit(-1)
if quant < 2:
quant = 2
if quant > 256:
quant = 256
# print info --------------------------------------------------------
print('source_directory = %s' % src_dir )
print('destination_directory = %s' % dst_dir )
print('quant = %d' % quant )
print()
# If dst_dir not exist, create it. If already exist, ask user whether to continue --------------------------------------------------------
if os.path.isdir(dst_dir) :
if input('warning: destination_directory %s already exist! continue? (y|n)' % dst_dir) != 'y':
exit(-1)
else:
os.mkdir(dst_dir)
# statistics variables -----------------------------
total_count = 0
total_src_size = 0
total_dst_size = 0
# for all files in src_dir, try to compress it, and save to dst_dir --------------------------------------------------------
for fname in os.listdir(src_dir) :
fname_prefix, _ = os.path.splitext(fname)
dst_fname = dst_dir + os.path.sep + fname_prefix + '.png'
src_fname = src_dir + os.path.sep + fname
# try to open file as image -----------------------------
try :
src_size = os.path.getsize(src_fname)
img_array2d = loadImageFileAsRGBArray(src_fname)
except :
print('%s skip' % fname)
continue
# get image height and width -----------------------------
h, w, _ = img_array2d.shape
# compress stage1 : use KMeans to quantize image and save as palette-PNG file (lossy) -----------------------------
print('compressing stage1 (KMeans Quantize) ...')
n_iter = saveImageAsPalettePNGusingKMeans(quant, img_array2d, dst_fname)
# compress stage2 : use optipng to do an optimization of deflate coding (lossless) -----------------------------
print('compressing stage2 (OptiPNG) ...')
callOptiPNG (optipng_path, dst_fname)
#command = '%s -o7 "%s"' % (optipng_path, dst_fname)
#os.system(command)
# get compressed PNG size -----------------------------
dst_size = os.path.getsize(dst_fname)
# decide whether to use the compressed file, or just copy the original file
if src_size <= dst_size : # not even better than original
if src_dir != dst_dir :
os.remove(dst_fname) # delete it
dst_fname = dst_dir + os.path.sep + fname # construct copy file name
copy_file(src_fname, dst_fname) # just copy file
dst_size = src_size
print('%s (%dx%d) just copy' % (fname, w, h) )
else :
print('%s (%dx%d) kmeans-iter=%d %dB -> %dB %.2f%% off!' % (fname, w, h, n_iter, src_size, dst_size, 100.0*(1.0-dst_size/src_size) ) )
total_count += 1
total_src_size += src_size
total_dst_size += dst_size
print()
if total_count > 0 :
print('%d files compressed %dB -> %dB %.2f%% off!' % (total_count, total_src_size, total_dst_size, 100.0*(1.0-total_dst_size/total_src_size) ) )
else :
print('no input file')