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.
# A tool to resize a set of pictures at once
scale_by = ARGV.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)
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 (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.