ImageMagick resize images  

ImageMagick has some pretty cool commandline tools to manage images. Specifically I wanted to resize an image (make it smaller).

Code:

convert -resize 50% -quality 80 input.jpg output.jpg
You can change resize to a pixel size also: 800x600. Pretty handy and quick.

Update: You can resize/recompress and entire directory of images using find and xargs. This works good if you need to resize an entire directory/camera full of images to save space.

Code:

find . -iname "*.jpg" | xargs -l -i convert -quality 75 {} /tmp/output/{}
Leave A Reply - 11 Replies
Replies
b 2005-07-25 05:08am - No Email - Logged IP: 163.1.241.60
find . -name "*.jpg" -exec convert -quality 75 {} /tmp/output/{} \;

achieves the same thing
Shantanoo 2005-08-31 02:46am - No Email - Logged IP: 143.127.3.10
find . -iname "*.jpg" | perl -e 'for(<>) { chomp $_;@a=split(/\//,$_);$str = "convert -resize 50% -quality 80 $_ thumbnails/".$a[-1];print $str."\n";`$str`}'
ozstriker78 2006-01-06 04:32pm - ozstriker78 - Logged IP: 209.104.55.5
anyone knows that using xargs is much more effecient than using the -exec find flag.
T 2006-01-21 02:41pm - No Email - Logged IP: 81.190.99.119
also you can do:
for i in `ls *.jpg`; do convert -resize 50% -quality 80 $1 conv_$1; done
Jeremy 2006-05-17 05:26am - No Email - Logged IP: 208.177.203.66
> also you can do:
> for i in `ls *.jpg`; do convert -resize 50% -quality 80 $1 conv_$1; done

$1 won't work at the end there, i think you mean to use $i like this:

for i in `ls *.jpg`; do convert -resize 50% -quality 80 $i conv_$i; done
Brad G- 2006-09-22 11:41pm - No Email - Logged IP: 216.102.106.153
How can I use the utility to dynamicly resize the images during display, i.e. in the perl code of my scipt that displays a dynamically created web page. I have hundreds of logos to display and I don't want to create additional copies for each display size. I just want to take the orginal 144x120px image and display it at 75% or so. IE and other browsers do a lousy job when you just tell them to display the 144x120 image at...say 120x100. They look like.....bad.
Olly 2006-10-04 02:05am - No Email - Logged IP: 155.245.65.39
Surely you can just just do this:

convert * -resize 30x30 -quality 100 scaled.jpg


Jocke 2007-02-04 11:46am - No Email - Logged IP: 91.84.13.251
> > also you can do:
> > for i in `ls *.jpg`; do convert -resize 50% -quality 80 $1 conv_$1; done

> $1 won't work at the end there, i think you mean to use $i like this:
> for i in `ls *.jpg`; do convert -resize 50% -quality 80 $i conv_$i; done

I couldn't make this one work with filenames with spaces, even if i added -b flag to LS which escapes spaces (" " becomes "\ ").
However, if you want to list both JPG and jpg files, simply add both to the ls command, as such: "ls *.jpg *.JPG" and you'll list both types.

Timmu 2007-06-12 01:35am - No Email - Logged IP: 212.7.30.66
Ahem. for i in *.jpg
mfajardo 2007-07-25 02:32pm - No Email - Logged IP: 209.120.159.47
To answer a previous question, you can resize images on the fly using phpThumb. I works very well.

www.munkeefarm.com

Slim 2007-07-31 09:45am - No Email - Logged IP: 66.46.243.35
Nice. Some comments and additional examples:

1. Use -print0 (and -0) to avoid problems with filenames containing Carriage Returns.
2. Use -maxdepth 0 to avoid descending into subdirectories. Use -maxdepth 1 to descend one level.
3. -i is deprecated for -I {}
4. -l is deprecated in preference for -L 1.
5. -I implies -L 1

find . -maxdepth 0 -iname "*.jpg" -print0 | xargs -0 -I {} convert -quality 75 {} /tmp/thumbs/{}

To compress and resize inplace:

NOTE! Mogrify is a dangerous command because it operates on the original files! Test your commands on COPIES of your files.

find . -maxdepth 0 -iname "*.jpg" -print0 | xargs -0 mogrify -resize 800x600 -quality 75

To change file types:

find . -maxdepth 0 -iname "*.jpg" -print0 | xargs -0 mogrify -format png

The above statement will leave the original JPG files alone, and write PNGs beside them.

To make PNG Thumbnails

*Before IM v6.3.4-3 the "-format" and "-path" settings were mutually exclusive. Upgrade if you need to use a different directory as well as a different image format for your output.

find . -maxdepth 0 -iname "*.jpg" -print0 | xargs -0 mogrify -path /tmp/thumbs -thumbnail 120x120 -quality 75 -format png

All content licensed under the Creative Commons License