Skip to content

Instantly share code, notes, and snippets.

@thecwlzone
Created February 17, 2020 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thecwlzone/fb59f5ceb06f1fb55bcbef2cafe41b5f to your computer and use it in GitHub Desktop.
Save thecwlzone/fb59f5ceb06f1fb55bcbef2cafe41b5f to your computer and use it in GitHub Desktop.
Use MiniMagick gem to reduce photo image sizes for use in web pages
#!/usr/bin/env ruby
# Photos from phones are usually very large (MegaBytes), so
# use MiniMagick to automate a file size reduction suitable for
# display as an image on a web page.
# References:
# https://github.com/minimagick/minimagick
# https://www.rubyguides.com/2018/12/minimagick-gem/
require "mini_magick"
# The photos to be processed are assumed to be in the current directory
image_list = `ls -1`.split("\n")
# Photos from phones often have spaces (" ") in the name, e.g. "xyz 123.JPG"
# Change spaces to underscores ("_")
# We want to keep the original photo, so use the open method and
# write a new file as "xyz_123_r.JPG" (r for "reduced")
image_list.each do |image|
renamed_image = image.gsub(" ", "_").upcase
reduced_image = renamed_image.gsub(".JPG", "_r.JPG")
puts "Processing #{image}"
working_image = MiniMagick::Image.open(image)
# These settings were determined by trial and error, YMMV
working_image.combine_options do |o|
o.strip
o.geometry 960
o.quality 50
end
working_image.write(reduced_image)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment