In the previous post :"How to resize an image using Ruby" I made a program that can be used to resize a single image using the Ruby Language and RMagick.
Now I want to extend this program to resize a variable set of pictures, and I also want to set the resizing rate at run time.
So I need to interpret the command line arguments. Ruby can handle the CLI arguments in a way very close to C language.
I want to write a program with the following synopsis:
resizer <scale_ratio> <list of files to resize>
where scale_ratio is the amount of scaling you want to apply: [.01…0.9999] reduces the size >1 increases the size.
#
# www.xappsoftware.com
# resizer.rb
# A tool to resize a set of pictures at once
#
require 'rubygems'
require 'RMagick'
numPar=ARGV.length
scale_by = ARGV[0].to_f
scale_by = scale_by/100
for i in 1..numPar-1
image = Magick::Image.read(ARGV[i]).first
new_image = image.scale(scale_by)
new_image.write(+scale_by.to_s+ARGV[i])
end
The Ruby language reads the CLI argument trough the ARGV array, it doesn't have the C argc parameter, but it is very simple to obtain from the ARGV array. Simply get the length of the array and you will have the corresponding argc parameter.
To get the scale ratio I have to convert the ARGV[0] (the first CLI parameter) to a float value, using the to_f method of the Ruby's strings.
Looping on the script of the previous article will do the job.
Gg1