Skip to content

Instantly share code, notes, and snippets.

@cheerfulstoic
Created January 27, 2016 13:51
Show Gist options
  • Save cheerfulstoic/1e64872d6c400eca1e61 to your computer and use it in GitHub Desktop.
Save cheerfulstoic/1e64872d6c400eca1e61 to your computer and use it in GitHub Desktop.
pngquant script
#!/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