When dealing with a lot of pictures, sometimes we want to make a common modification such as a change of resolution. In the case of multiple screenshots it is also common to crop all of the images at a fixed position (such as selecting only the same window). Here we will take a look at the command using ImageMagick mogrify.
Doing this job by hand, even just for three or four times, using the same parameters, is stressful and tiring, so we can ask for help to the terminal!
Mogrify is a command of the ImageMagick package and allows us to perform many operations on multiple images. In addition to scaling, resizing and cropping, mogrify can compress, convert, apply filter to images and much more.
$ sudo yum install ImageMagick
$ sudo apt-get install ImageMagick
The program overwrites the existing images, so, in order to prevent any unwanted modification, make a backup of the images or at least try the command on a sample image to make sure that it is the wanted modification.
Firstly, enter in the directory where the images that we want to modify are
$ cd folder_name
Then, say you want to scale the pictures to a resolution of 640×480:
$ mogrify -scale 640×480 *.*
(note: if using the wildcard *.* , every single file in the folder will be modified by the command, so make sure that the folder contains only the files you want to modify or use different wildcards as *.jpg or e.g. *-2012.jpg)
If you want to scale the pictures keeping the aspect ratio, specify only the length in pixels and the height will be adjusted to keep constant the aspect ratio:
$ mogrify -scale 640 *.*
Or using the same argument for the height instead of the width:
$ mogrify -scale x480 *.*
Or can also use a percentage of the original size, so if you want to halve the resolution:
$ mogrify -scale 50% *.*
Or you can even modify only pictures bigger (or smaller) than the specified resolution and keep the others unchanged, respectively:
$ mogrify -scale ‘640×480>’ *.*
$ mogrify -scale ‘640×480<‘ *.*
If you want to crop pictures in a window of 300×200 starting at pixels x=0 and y=100 where x is the offset in length from left to right and y is the offset in height from top to bottom, you need to use another option.
Since it is difficult to know the offset precise values a priori using gimp (a graphical editor) to take these values from the cursor position or selection can help. Then, use:
$ mogrify -crop 300×200+0+100 *.*
If you want to compress images, use:
$ mogrify -compress JPEG -quality 85 *.jpg
where JPEG is the compression type and 85 is the quality for JPEG compression, which varies from 0 (lowest) to 100 (highest).
Or convert images from gif to jpeg format:
$ mogrify -format jpg -quality 85 *.gif
And rotate by an angle, e.g. 90 degrees:
$ mogrify -rotate 90 *.*
Or if you want to rotate the image only in specific occasions, as if height is bigger than length (e.g. 480×640) (and viceversa), use, respectively:
$ mogrify -rotate ’90>’ *.*
$ mogrify -rotate ’90<‘ *.*
Check out the official page of ImageMagick on Mogrify here, where you can find with more detail all the options of mogrify.