/
optimize-png
executable file
·76 lines (63 loc) · 2.3 KB
/
optimize-png
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
#!/usr/bin/env ruby
# pngcrush -rem allb -reduce -brute original.png optimized.png
# pngcrush -d target-dir/ *.png
#
# -rem allb — remove all extraneous data (Except transparency and gamma; to remove everything except transparency, try -rem alla)
# -reduce — eliminate unused colors and reduce bit-depth (If possible)
#
# -brute — attempt all optimization methods (Requires MUCH MORE processing time and may not improve optimization by much)
#
# original.png — the name of the original (unoptimized) PNG file
# optimized.png — the name of the new, optimized PNG file
# -d target-dir/ — bulk convert into this directory "target-dir"
#
# -rem cHRM -rem sRGB -rem gAMA -rem ICC — remove color profiles by name (shortcut -rem alla)
#
# An article explaining why removing gamma correction
# http://hsivonen.iki.fi/png-gamma/
def indented_puts(str="")
puts " #{str}"
end
def print_manual
script_name = "optimize-png"
indented_puts
indented_puts script_name
indented_puts "=" *script_name.size
indented_puts "- May reduce PNG file size lossless"
indented_puts "- Removes color profiles: cHRM, sRGB, gAMA, ICC, etc."
indented_puts "- Eliminates unused colors and reduce bit-depth (If possible)"
indented_puts
indented_puts "Batch optimize all *.png in current directory:"
indented_puts " #{script_name} output_directory"
indented_puts
indented_puts "Optimize single file:"
indented_puts " #{script_name} input.png output.png"
indented_puts
end
def optimization_default_args
args = ""
args << "-rem alla " # remove everything except transparency
args << "-rem text " # remove text chunks
args << "-reduce " # eliminate unused colors and reduce bit-depth (If possible)
args
end
def batch_optimize(script_args)
output_directory = script_args[0]
match_pattern = "*.*" # be aware of case sensitivty: *.png wouldn't match IMAGE.PNG # pngcrush does nothing with other files than png
exec "pngcrush -d #{output_directory} #{optimization_default_args} #{match_pattern}"
end
def single_file_optimize(script_args)
input_file = script_args[0]
output_file = script_args[1]
exec "pngcrush #{optimization_default_args} #{input_file} #{output_file}"
end
def main
if ARGV.size == 1
batch_optimize(ARGV)
elsif ARGV.size == 2
single_file_optimize(ARGV)
else
print_manual
end
end
main