Created
August 2, 2012 05:06
-
-
Save sholsinger/3233833 to your computer and use it in GitHub Desktop.
Quick and Dirty Image Resizing with Ruby & RMagick
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/ruby | |
require "RMagick" | |
# ensure dirs are available for writing | |
["thumbs","full","processed"].each do |dir| | |
if !File.directory? dir | |
Dir.mkdir dir | |
end | |
end | |
# Loop through items in the dir | |
files = Dir.entries "." | |
fileCounter = 1 | |
totalFiles = files.length-2 | |
files.each do |f| | |
if File.directory? f | |
puts "Skipping directory: #{f}" | |
next | |
end | |
if [".", "..", ".DS_Store"].index(f) == nil | |
puts "Resizing file: #{f} - #{fileCounter} of #{totalFiles}" | |
img = Magick::Image.read(f).first | |
thumb = img.resize_to_fit(100, 100) | |
full = img.resize_to_fit(580, 580) | |
thumb.write("thumbs/#{f}") {self.quality = 60} | |
full.write("full/#{f}") {self.quality = 75} | |
fileCounter += 1 | |
# move the file to processed folder | |
system("mv \"#{f}\" processed/") | |
end | |
sleep 0.5 | |
end | |
system("growlnotify -t 'Ruby' -m 'Finshed resizing images!'") | |
puts "Done." |
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/ruby | |
require "RMagick" # image processing wrapper for ImageMagick | |
require "Parallel" # https://github.com/grosser/parallel | |
# ensure dirs are available for writing | |
["thumbs","full","processed"].each do |dir| | |
if !File.directory? dir | |
puts "Creating #{dir}/" | |
Dir.mkdir dir | |
end | |
end | |
# encapsulates the image resizing logic | |
def resizeImage(f) | |
# get the first frame of the image (JPGs usually have only one) | |
img = Magick::Image.read(f).first | |
# prep thumb and full sizes | |
thumb = img.resize_to_fit(100, 100) | |
full = img.resize_to_fit(580, 580) | |
# write resized images | |
thumb.write("thumbs/#{f}") {self.quality = 60} | |
full.write("full/#{f}") {self.quality = 75} | |
# free up RAM | |
img.destroy! | |
thumb.destroy! | |
full.destroy! | |
end | |
# Loop through items in the dir | |
files = Dir.entries "." | |
# clean file list of known non-files | |
files.delete_if {|f| [".", "..", ".DS_Store"].index(f) != nil or File.directory?(f) } | |
# let the user know we've begun... | |
puts ("Resizing #{files.length} files...") | |
# process the images in parallel threads (up to 4) | |
completed = Parallel.each(files){|file| | |
if File.file? file | |
resizeImage(file) | |
# move the file to processed folder | |
system("mv \"#{file}\" processed/") | |
# micro-sleep so other processes can get some CPU time | |
sleep 0.3 | |
end | |
} | |
# Show a growl notification if available. | |
system("growlnotify --appIcon Terminal -m 'Finshed resizing images!' Ruby") | |
puts "Finished resizing #{completed.length} images" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
image_resize.rb
This is the initial Quick & Dirty script. It goes one image at a time.
image_resize_mt.rb
This is the revised multi-process script which speeds things up considerably depending on the number of CPUs your machine has. It has better memory management and doesn't spam STDOUT as much as the Q&D version. This version depends on Parallel in addition to RMagick.