re-size images from the Linux command line

I keep a repository of photographs connected to my server, and from it will copy images over into websites I am drafting. In their original format, these images are sized far in excess of what is necessary for high-definition (HD) viewing on the web, and page loading speeds are incredibly slow. To re-size them, I use a few simple ImageMagick commands (no GUI needed)!

re-sizing with ImageMagick's convert

A simple convert command can be used to define a resize value in pixels. This operation produces a copy of the original file with a different name:

$ convert fig-1.png -resize 550x fig-1-s.png

With this command, the x signifier after the pixel value (with no space) explicity defines image width. To specify the height, just prefix the pixel value with an x, like this:

$ convert fig-1.png -resize x250 fig-1-s.png

By specifying only the height or width of an image, the non-specified values are proportionally adjusted automatically.

ImageMagick's convert can be used on a number of images at the same time (i.e., a batch process) with scripting.

For example, to reduce the size of an image and to rename it and change the uppercase '.JPG' extension to lower case '.jpg', you would write a script such as follows, and then execute it with the sh command:

for i in `ls *.JPG`; do  
    convert $i -resize 10% ${i%.JPG}.jpg;

NOTE: Make sure to give your script a sensible name to recognize it later on, e.g.,

Change the script to suit your needs, of course. You can, for instance, define your desired image size in pixels by replacing the 10% value (e.g., 400x600). To change the output file format to something other than .jpg, just plug it where .jpg is given in the example above. [ImageMagick can read, convert and write images in a variety of formats (about 100) including GIF, JPEG, JPEG-2000, PNG, PDF, PhotoCD, TIFF and DPX.] To rename your converted files, add some form of identifier, such as -s (for 'small').

Here's another example:

for i in `ls *.JPG`; do  
    convert $i -resize 500x ${i%.JPG}-s.png;

When scripting, be aware that filenames must not contain any space, and that the filename must match to the first two “.JPG” or “.jpg”. Also keep in mind that the script will resize all the pictures in the current folder, but not in any sub folders.

re-sizing with ImageMagick's mogrify

The mogrify tool is similar to convert except that the original image file is overwritten (unless you change the file suffix with the -format option).

Here are some exaples:

To reduce the size of an image by percentage:

mogrify -resize 50% image.jpg

To resize all .jpg images in a folder to a maximum dimension of 256x256, the command would be as follows:

mogrify -resize 256x256 *.jpg

Mogrify outputs can be written to a target directory as follows:

mogrify -resize 600x900 -quality 100 -path ../new-images *.jpg

Mogrify can also be used to convert images from one file type to another by using -format, such as follows:

mogrify -format jpg *.png


View or Post Comments