Skip to content

Instantly share code, notes, and snippets.

@guidipolito
Created April 12, 2024 22:36
Show Gist options
  • Save guidipolito/fe311b5c04fc8f4486d610604a35b1ae to your computer and use it in GitHub Desktop.
Save guidipolito/fe311b5c04fc8f4486d610604a35b1ae to your computer and use it in GitHub Desktop.
optimize all png and jpeg inside the actual directory
# image_optimizer requires availability of
# - pngquant
# - optipng
# - jpegoptim
require "image_optimizer"
require 'parallel'
def image?(file)
file =~ /\.(jpe?g|png)$/
end
files = `find`.split("\n").filter do |file|
image?(file)
end
def optimize(files)
files.each do |f|
# quality is measure for jpeg, level is the measure for png
ImageOptimizer.new(f, quality: 80, level: 3).optimize
end
end
Parallel.map(files.each_slice(10).to_a, in_processes: 6) { |f| optimize(f) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment