2011-02-25

The Need

Today I started putting together a little image gallery for my sideburns and I really wanted a way to easily scale a directory of images to a specific height, save the result in a directory and then repeat the process and save in yet another directory.

The Solution

There is probably an easy way to do what I want using GIMP, but I thought it would be better if I wrote a script to handle this for me. Technically, the script that I wrote is just a fancy wrapper for the imagemagick convert command.

Enter the Ruby

#!/usr/bin/env ruby

#create a function to end the script with a message
def exit_help(message=nil)
  #did a message get passed in?
  if !message.nil?
    #print the message
    puts message
  end
  #print the 'help'
  puts "--Help--
#{$0path/to/image/directory thumbnail_height image_height  
  "
  exit()
end

#make a function to create a dir if it doesn't exist
def check_dir(dir)
  if !File.directory?dir )
  Dir.mkdir(dir)
  end
end

#make a function to run a set command
def do_command(comm)
  puts comm
  system(comm)
end

#set some variable  based on the input from the user
start_dir ARGV[0]
thumb_height ARGV[1].to_i
image_height ARGV[2].to_i

#if any of the vars aren't set, the user didn't input enough info
if start_dir.nil? or thumb_height==or image_height==0
  exit_help()
end

#does the start dir exist?
if !File.directory?start_dir )
  exit_help"#{start_dirdoes not exist" )
end

# define the output dirs
gallery_dir File.join(start_dir,'gallery')
thumb_dir File.join(start_dir,'gallery_thumbs')

# make the dirs if they don't exist
check_dir(gallery_dir)
check_dir(thumb_dir)

#loop through the files in the start_dir
start_dir_files File.join(start_dir,"*.{png,jpg}")
Dir.glob(start_dir_files)  do |file|
  #what is the name of the file?
  fname File.basename(file)
  #what is the path of the new gallery file?
  gfile File.join(gallery_dir,fname)
  #what is the path of the new thumbnail file?
  tfile File.join(thumb_dir,fname)
  #what imagemagick commands will resize the images? 
  c1 "convert -resize x#{image_height} #{file} #{gfile}"
  c2 "convert -resize x#{thumb_height} #{file} #{tfile}"
  #run the commands
  do_commandc1 )
  do_commandc2 )
end

What It Do?

I'll tell you what it do! When run, the script requires three arguments: 1. The name of the directory containing the images 2. the height of the thumbnail image 3. the height of the gallery image

The end result will be a directory named "gallery" and a directory named "gallery_thumbs" in the images directory. Gallery will contain the images resized to the gallery height specifications. I'll leave it to you to figure out what images are in the "gallery_thumbs" directory.

My Take Away

Originally, I had planned on using the exec() function to run my convert commands but exec() replaces the calling script with the command being called. This means that as soon as I ran exec(), my code would quit running. Oh well, system() works just fine for my needs.

Comments
Name:
not required
Email:
not required (will not be displayed)
Website:
not required (will link your name to your site)
Comment:
required
Please do not post HTML code or bbcode unless you want it to show up as code in your post. (or if you are a blog spammer, in which case, you probably aren't reading this anyway).
Prove you are human by solving a math problem! I'm sorry, but due to an increase of blog spam, I've had to implement a CAPTCHA.
Problem:
1 plus 4
Answer:
required
subscribe
 
2016
2015
2014
2013
2012
2011
2010
December
November
October
September
August
July
June
May
April
March
February
January
2009
December
November
October
September
August
July
June
May
April
March
February
January
2008