56
56
Source: https://github.com/chrissimpkins/Crunch
57
57
==================================================
58
58
59
- crunch is a command line executable that performs lossy optimization of one or more png image files with pngquant and zopflipng.
59
+ crunch is a command line executable that performs lossy optimization of one
60
+ or more png image files with pngquant and zopflipng.
60
61
61
62
Usage:
62
63
$ crunch [image path 1]...[image path n]
@@ -95,8 +96,8 @@ def main(argv):
95
96
96
97
if len (argv ) == 0 :
97
98
sys .stderr .write (
98
- ERROR_STRING + " Please include one or more paths to PNG image files as "
99
- "arguments to the script." + os .linesep
99
+ f" { ERROR_STRING } Please include one or more paths to PNG image files as "
100
+ "arguments to the script.{ os.linesep}"
100
101
)
101
102
sys .exit (1 )
102
103
@@ -138,63 +139,50 @@ def main(argv):
138
139
# Not a file test
139
140
if not os .path .isfile (png_path ): # is not an existing file
140
141
sys .stderr .write (
141
- ERROR_STRING
142
- + " '"
143
- + png_path
144
- + "' does not appear to be a valid path to a PNG file"
145
- + os .linesep
142
+ f"{ ERROR_STRING } '{ png_path } ' does not appear to be a "
143
+ f"valid path to a PNG file{ os .linesep } "
146
144
)
147
145
sys .exit (1 ) # not a file, abort immediately
148
146
# PNG validity test
149
147
if not is_valid_png (png_path ):
150
148
sys .stderr .write (
151
- ERROR_STRING
152
- + " '"
153
- + png_path
154
- + "' is not a valid PNG file."
155
- + os .linesep
149
+ f"{ ERROR_STRING } '{ png_path } ' is not a valid PNG file.{ os .linesep } "
156
150
)
157
151
if is_gui (argv ):
158
- log_error (png_path + " is not a valid PNG file." )
152
+ log_error (f" { png_path } is not a valid PNG file." )
159
153
NOTPNG_ERROR_FOUND = True
160
154
161
- # Exit after checking all file requests and reporting on all invalid file paths (above)
155
+ # Exit after checking all file requests and reporting on all invalid
156
+ # file paths (above)
162
157
if NOTPNG_ERROR_FOUND is True :
163
158
sys .stderr .write (
164
- "The request was not executed successfully. Please try again with one or more valid PNG files. "
165
- + os .linesep
159
+ f "The request was not executed successfully. Please try again "
160
+ f"with one or more valid PNG files. { os .linesep } "
166
161
)
167
162
if is_gui (argv ):
168
163
log_error (
169
- "The request was not executed successfully. Please try again with one or more valid PNG files."
164
+ "The request was not executed successfully. Please try "
165
+ "again with one or more valid PNG files."
170
166
)
171
167
sys .exit (1 )
172
168
173
169
# Dependency error handling
174
170
if not os .path .exists (PNGQUANT_EXE_PATH ):
175
171
sys .stderr .write (
176
- ERROR_STRING
177
- + " pngquant executable was not identified on path '"
178
- + PNGQUANT_EXE_PATH
179
- + "'"
180
- + os .linesep
172
+ f"{ ERROR_STRING } pngquant executable was not identified on path "
173
+ f"'{ PNGQUANT_EXE_PATH } '{ os .linesep } "
181
174
)
182
175
if is_gui (argv ):
183
- log_error (
184
- "pngquant was not found on the expected path " + PNGQUANT_EXE_PATH
185
- )
176
+ log_error (f"pngquant was not found on the expected path { PNGQUANT_EXE_PATH } " )
186
177
sys .exit (1 )
187
178
elif not os .path .exists (ZOPFLIPNG_EXE_PATH ):
188
179
sys .stderr .write (
189
- ERROR_STRING
190
- + " zopflipng executable was not identified on path '"
191
- + ZOPFLIPNG_EXE_PATH
192
- + "'"
193
- + os .linesep
180
+ f"{ ERROR_STRING } zopflipng executable was not identified on path "
181
+ f"'{ ZOPFLIPNG_EXE_PATH } '{ os .linesep } "
194
182
)
195
183
if is_gui (argv ):
196
184
log_error (
197
- "zopflipng was not found on the expected path " + ZOPFLIPNG_EXE_PATH
185
+ f "zopflipng was not found on the expected path { ZOPFLIPNG_EXE_PATH } "
198
186
)
199
187
sys .exit (1 )
200
188
@@ -208,33 +196,31 @@ def main(argv):
208
196
optimize_png (png_path_list [0 ])
209
197
else :
210
198
processes = PROCESSES
211
- # if not defined by user, start by defining spawned processes as number of available cores
199
+ # if not defined by user, start by defining spawned processes as number
200
+ # of available cores
212
201
if processes == 0 :
213
202
processes = cpu_count ()
214
203
215
- # if total cores available is greater than number of files requested, limit to the latter number
204
+ # if total cores available is greater than number of files requested,
205
+ # limit to the latter number
216
206
if processes > len (png_path_list ):
217
207
processes = len (png_path_list )
218
208
219
209
print (
220
- "Spawning "
221
- + str (processes )
222
- + " processes to optimize "
223
- + str (len (png_path_list ))
224
- + " image files..."
210
+ f"Spawning { processes } processes to optimize { len (png_path_list )} "
211
+ f"image files..."
225
212
)
226
213
p = Pool (processes )
227
214
try :
228
215
p .map (optimize_png , png_path_list )
229
216
except Exception as e :
230
217
stdstream_lock .acquire ()
231
- sys .stderr .write ("-----" + os .linesep )
218
+ sys .stderr .write (f "-----{ os .linesep } " )
232
219
sys .stderr .write (
233
- ERROR_STRING
234
- + " Error detected during execution of the request."
235
- + os .linesep
220
+ f"{ ERROR_STRING } Error detected during execution of the request."
221
+ f"{ os .linesep } "
236
222
)
237
- sys .stderr .write (str ( e ) + os .linesep )
223
+ sys .stderr .write (f" { e } { os .linesep } " )
238
224
stdstream_lock .release ()
239
225
if is_gui (argv ):
240
226
log_error (str (e ))
@@ -272,49 +258,40 @@ def optimize_png(png_path):
272
258
pngquant_options = (
273
259
" --quality=80-98 --skip-if-larger --force --strip --speed 1 --ext -crunch.png "
274
260
)
275
- pngquant_command = (
276
- PNGQUANT_EXE_PATH + pngquant_options + shellquote (img .pre_filepath )
277
- )
261
+ pngquant_command = PNGQUANT_EXE_PATH + pngquant_options + shellquote (img .pre_filepath )
278
262
try :
279
263
subprocess .check_output (pngquant_command , stderr = subprocess .STDOUT , shell = True )
280
264
except CalledProcessError as cpe :
281
265
if cpe .returncode == 98 :
282
266
# this is the status code when file size increases with execution of pngquant.
283
- # ignore at this stage, original file copied at beginning of zopflipng processing
284
- # below if it is not present due to these errors
267
+ # ignore at this stage, original file copied at beginning of zopflipng
268
+ # processing below if it is not present due to these errors
285
269
pass
286
270
elif cpe .returncode == 99 :
287
271
# this is the status code when the image quality falls below the set min value
288
- # ignore at this stage, original lfile copied at beginning of zopflipng processing
289
- # below if it is not present to these errors
272
+ # ignore at this stage, original lfile copied at beginning of zopflipng
273
+ # processing below if it is not present to these errors
290
274
pass
291
275
else :
292
276
stdstream_lock .acquire ()
293
277
sys .stderr .write (
294
- ERROR_STRING
295
- + " "
296
- + img .pre_filepath
297
- + " processing failed at the pngquant stage."
298
- + os .linesep
278
+ f"{ ERROR_STRING } { img .pre_filepath } processing failed at the pngquant "
279
+ f"stage.{ os .linesep } "
299
280
)
300
281
stdstream_lock .release ()
301
282
if is_gui (sys .argv ):
302
283
log_error (
303
- img .pre_filepath
304
- + " processing failed at the pngquant stage. "
305
- + os .linesep
306
- + str (cpe )
284
+ f"{ img .pre_filepath } processing failed at the pngquant stage."
285
+ f"{ os .linesep } { cpe } "
307
286
)
308
287
return None
309
288
else :
310
289
raise cpe
311
290
except Exception as e :
312
291
if is_gui (sys .argv ):
313
292
log_error (
314
- img .pre_filepath
315
- + " processing failed at the pngquant stage. "
316
- + os .linesep
317
- + str (e )
293
+ f"{ img .pre_filepath } processing failed at the pngquant stage."
294
+ f"{ os .linesep } { e } "
318
295
)
319
296
return None
320
297
else :
@@ -329,9 +306,10 @@ def optimize_png(png_path):
329
306
# pngquant does not write expected file path if the file was larger after processing
330
307
if not os .path .exists (img .post_filepath ):
331
308
shutil .copy (img .pre_filepath , img .post_filepath )
332
- # If pngquant did not quantize the file, permit zopflipng to attempt compression with mulitple
333
- # filters. This achieves better compression than the default approach for non-quantized PNG
334
- # files, but takes significantly longer (based upon testing by CS)
309
+ # If pngquant did not quantize the file, permit zopflipng to attempt compression
310
+ # with mulitple filters. This achieves better compression than the default
311
+ # approach for non-quantized PNG files, but takes significantly longer
312
+ # (based upon testing by CS)
335
313
zopflipng_options = " -y --lossy_transparent "
336
314
zopflipng_command = (
337
315
ZOPFLIPNG_EXE_PATH
@@ -345,30 +323,23 @@ def optimize_png(png_path):
345
323
except CalledProcessError as cpe :
346
324
stdstream_lock .acquire ()
347
325
sys .stderr .write (
348
- ERROR_STRING
349
- + " "
350
- + img .pre_filepath
351
- + " processing failed at the zopflipng stage."
352
- + os .linesep
326
+ f"{ ERROR_STRING } { img .pre_filepath } processing failed at the zopflipng "
327
+ f"stage.{ os .linesep } "
353
328
)
354
329
stdstream_lock .release ()
355
330
if is_gui (sys .argv ):
356
331
log_error (
357
- img .pre_filepath
358
- + " processing failed at the zopflipng stage. "
359
- + os .linesep
360
- + str (cpe )
332
+ f"{ img .pre_filepath } processing failed at the zopflipng stage."
333
+ f"{ os .linesep } { cpe } "
361
334
)
362
335
return None
363
336
else :
364
337
raise cpe
365
338
except Exception as e :
366
339
if is_gui (sys .argv ):
367
340
log_error (
368
- img .pre_filepath
369
- + " processing failed at the pngquant stage. "
370
- + os .linesep
371
- + str (e )
341
+ f"{ img .pre_filepath } processing failed at the zopflipng stage."
342
+ f"{ os .linesep } { e } "
372
343
)
373
344
return None
374
345
else :
@@ -383,7 +354,8 @@ def optimize_png(png_path):
383
354
if not is_gui (sys .argv ) and percent < 100 :
384
355
percent_string = format_ansi_green (percent_string )
385
356
386
- # report percent original file size / post file path / size (bytes) to stdout (command line executable)
357
+ # report percent original file size / post file path / size (bytes) to
358
+ # stdout (command line executable)
387
359
stdstream_lock .acquire ()
388
360
print (
389
361
"[ "
@@ -396,7 +368,8 @@ def optimize_png(png_path):
396
368
)
397
369
stdstream_lock .release ()
398
370
399
- # report percent original file size / post file path / size (bytes) to log file (macOS GUI + right-click service)
371
+ # report percent original file size / post file path / size (bytes) to log file
372
+ # (macOS GUI + right-click service)
400
373
if is_gui (sys .argv ):
401
374
log_info (
402
375
"[ "
0 commit comments