Created
January 27, 2016 13:51
-
-
Save cheerfulstoic/1e64872d6c400eca1e61 to your computer and use it in GitHub Desktop.
pngquant script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'bundler/setup' | |
require 'png_quantizator' | |
require 'tempfile' | |
speed = ENV['SPEED'] || 1 | |
ARGV.each do |png_path| | |
puts "Processing #{png_path}..." | |
file = PngQuantizator::Image.new(png_path) | |
old_size = File.size(png_path) | |
tempfile = Tempfile.new('pngquant') | |
file.quantize_to(tempfile.path) | |
new_size = File.size(tempfile.path) | |
percentage = 100.0 * new_size / old_size | |
if percentage < 80 | |
puts "We can make it smaller, we have the technology!" | |
File.open(png_path, 'w') { |f| f << File.read(tempfile.path) } | |
puts "Shrunk to #{percentage.round(2)}% the size" | |
else | |
puts "Not shrinking (would only be #{percentage.round(2)}% the size)" | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment