Saturday, 22 August 2009

Tip of the Day: Resize Images from CLI Using ImageMagick

ImageMagick is a powerful set of tools used to perform various operations on images directly from the command-line. To install it in Debian type as root:

apt-get install imagemagick

Or in Ubuntu:

sudo apt-get install imagemagick

With the user password.

Resize images using convert
The tool we're going to need for this is convert, which is included in the imagemagick package. Here's one easy way to do it:

convert -sample 50%x50% input_image.png output_image.png

This will resize the input_image.png and make it half the original image.

Another tool which does the same thing is mogrify, with the exception that it will overwrite the original image:

mogrify -resize 320 filename.jpg

This will resize filename.jpg using a width of 320 and keeping the aspect ratio intact.


Anonymous said...

Nice post! Many of the imagemagick commands can be extended with a little shell code to perform work on a large number of files:

Resizing images while leaving the originals at their current size:
j=1; for i in *; do convert -resize 48x48 "$i" image$j.png; j=$((j+1)); done

Create thumbnails of images:
j=1; for i in *; do convert -thumbnail 128x128 "$i" thumbnail$j.png; j=$((j+1)); done

I just wrote a tutorial about this myself.. great minds think alike :)

Craciun Dan said...

Thanks for sharing these, Ian. Of course, these are very nice for a personal collection of scripts or one-liners for working with images.