-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptimize_png_for_web.py
56 lines (40 loc) · 2.13 KB
/
optimize_png_for_web.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
import sys
import argparse
import collections
from os import remove, path
from subprocess import call
def _compress_png(source_path, destination_path, is_quantization_allowed):
PNG_CRUSH_TOOL = "./lib/pngcrush_1_8_11_w64.exe"
PNG_QUANT_TOOL = "./lib/pngquant.exe"
png_crush_source = source_path
temporary_file = None
if path.isfile(destination_path):
remove(destination_path)
if is_quantization_allowed:
temporary_file = source_path + ".quant"
call([PNG_QUANT_TOOL, "--strip", "--quality=45-75", "--speed", "1", source_path, "-o", temporary_file])
png_crush_source = temporary_file
call([PNG_CRUSH_TOOL, "-rem", "alla", "-rem", "text", "-reduce", "-q", png_crush_source, destination_path])
if temporary_file:
remove(temporary_file)
def optimize_png(source_path, destination_path, quantization_blacklist_path):
with open(quantization_blacklist_path, 'r') as f:
blacklist = set(f.read().split())
prev_blacklist_path = quantization_blacklist_path + ".prev"
prev_blacklist = set()
if path.isfile(prev_blacklist_path):
with open(prev_blacklist_path, 'r') as f:
prev_blacklist = set(f.read().split())
can_quantize = source_path.lower() not in blacklist
quantized_last_time = source_path.lower() not in prev_blacklist if len(prev_blacklist) else can_quantize
if not path.isfile(destination_path) or \
path.getmtime(source_path) > path.getmtime(destination_path) or \
can_quantize != quantized_last_time:
_compress_png(source_path, destination_path, can_quantize)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='optimize a png file for the web')
parser.add_argument('--unquantizable_textures', "-u", help='path to file with a list of unquantizable textures', required=True)
parser.add_argument('--input', "-i", type=str, help='path to input png', required=True)
parser.add_argument('--output', "-o", type=str, help='path to output png', required=True)
args = parser.parse_args()
optimize_png(args.input, args.output, args.unquantizable_textures)