#!/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"