A quick way to resize images in Linux

Quite often i find myself wanting to resize a whole directory of images. Rather than opening them all in the gimp, i do it through the command line. I seem to do this often enough that it’s worth recording here, for my own reference as well as for anybody else who would find it useful.

First of all, you need imagemagick.

sudo apt-get install imagemagick

Change directory to where the images are and create a subdirectory for the resized versions.

cd ~/Photos/2010/04/05
mkdir resized

My phone likes to put spaces in file names which really confuses things, so i convert them to underscores. You can skip this step if your filenames contain no spaces.

for image in *.jpg; do mv "$image" `echo $image | tr ' ' '_'`; done

Now for the clever bit: i run convert command on the image files, resizing them to 1024px and saving with a quality of 80%.

for image in *.jpg; do convert $image -resize 1024 -quality 80 resized/$image; done

Lovely wonderful linux! :D

One thing to note: it always resizes along the top edge, which may not be the longest edge. If you have a portrait file which is 1536×2048 it comes out at 1024×1365 (not 768×1024 as you might have expected).

The resize option can take a percentage, so if you know all your images are the same size then you could just send a 50% to reduce to half size.

for image in *.jpg; do convert $image -resize 50% -quality 80 resized/$image; done

Imagemagick is super-incredible-awesome so there probably is a way to deal with differently sized images at different orientations. If anybody knows, please add it in the comments! :)

16 comments on “A quick way to resize images in Linux

  1. Great tip… i usually go over to my windows PC and do this as a batch in Photoshop… doing things on the command line is always much faster though!

  2. ah, perhaps that happens when the image contains transparency or an alpha channel. thanks for the warning. resizing images from my phone or camera always works, however.

  3. resizing the images in the commands is always faster than in designing. usually i used to do it one by one as the sizes for different images are different. But from the code snippet you have provided helps me alot in my task.

  4. Thats a great idea. no waste of time. Otherwise we have to to download the image then take in the photoshop and resize then again upload simply waste of time..

Leave a comment