Let us suppose you have an image named
caps-1.png and you want to crop it to a 450x760 area, where top left corner of the crop region is the point (613,130) in the original image. You can easily do this by using the
convert command that comes with ImageMagick. The command you might want to use might look like this:
convert "caps-1.png" -crop "450x760+613+130" "out/caps-1.png"
The numbers in the above are:
(613,130) - top left corner
450x760 - the crop area (x,y)
Now let us suppose you have a directory full of images that need to cropped to the same dimensions as above at the same point as above. You can use a little Ruby snippet to execute the same
convert command as above, but for each file in the current directory.
Dir.foreach('.'){|f| system 'convert \"%s\" -crop \"450x760+613+130\" \"out/%s\"' %[f, f.downcase] if f.downcase =~ /\.png$/}
This can be executed from the command line through the ruby interpreter:
ruby -e "Dir.foreach('.'){|f| system 'convert \"%s\" -crop \"450x760+613+130\" \"out/%s\"' %[f, f.downcase] if f.downcase =~ /\.png$/}"
If you want to simply generate script file with a series of
convert commands, then simply replace the Ruby "system" statement with a "puts" statement:
ruby -e "Dir.foreach('.'){|f| puts 'convert \"%s\" -crop \"450x760+613+130\" \"out/%s\"' %[f, f.downcase] if f.downcase =~ /\.png$/}" > convert_files.sh
There the
puts statements write the
convert commands with parameters to standard out (STDOUT), and that output is redirected to the file
convert_files.sh. Now you can set the executable bit of the script file
chmod +x convert_files.sh
and execute it over and over again.
If your source files are, say, PNG files but you want the
convert command to save the output as JPEG files, use the following:
ruby -e "Dir.foreach('.'){|f| system 'convert \"%s\" -crop \"450x760+613+130\" \"out/%s\"' %[f, f.downcase.sub('.png', '.jpg')] if f.downcase =~ /\.png$/}"
For more information take a look at the "Command Line Processing" section of the ImageMagick manual:
http://www.imagemagick.org/script/command-line-processing.php#geometry
The commands listed here were used to extract the areas of interest of frames grabbed from an iPhone application demo video.