ImageMagick resize images

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

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.

find . -iname "*.jpg" | xargs -l -i convert -quality 75 {} /tmp/output/{}
Leave A Reply - 19 Replies
Replies
July 25th 2005 - b

find . -name "*.jpg" -exec convert -quality 75 {} /tmp/output/{} \;

achieves the same thing

August 31st 2005 - Shantanoo

find . -iname "*.jpg" | perl -e 'for(<>) { chomp $;@a=split(/\//,$);$str = "convert -resize 50% -quality 80 $_ thumbnails/".$a[-1];print $str."\n";$str}'

January 6th 2006 - ozstriker78

anyone knows that using xargs is much more effecient than using the -exec find flag.

January 21st 2006 - T

also you can do: for i in ls *.jpg; do convert -resize 50% -quality 80 $1 conv_$1; done

May 17th 2006 - Jeremy

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

September 22nd 2006 - Brad G-

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.

October 4th 2006 - Olly

Surely you can just just do this:

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

February 4th 2007 - Jocke

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.

June 12th 2007 - Timmu

Ahem. for i in *.jpg

July 25th 2007 - mfajardo

To answer a previous question, you can resize images on the fly using phpThumb. I works very well.

www.munkeefarm.com

July 31st 2007 - Slim

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

March 25th 2009 - Walter

You don't need any complicated shell script to batch convert a whole directory of images - because ImageMagick's "convert" and "mogrify" commands support wildcards. (Aside: "convert" replaces existing files, "mogrify" allows you to specify an output file)

Here's an example of a single command that will create JPEG images at no larger than 640x480, setting the quality to 75 (pretty good). Notice the "-strip" option: This takes out JPEG's EXIF header data - which even in a small JPEG file can be 20,000 bytes of data! So it's a great idea to include this for web images, if you don't need all that EXIF data (and if you're like me, you probably didn't even know it existed, so you won't miss it right?)

mogrify -format jpg -quality 75 -resize "640x480>" -strip *

The wildcard "*" at the end means: Do all the files you find in this directory.

Enjoy :)

April 10th 2009 - Brad Jensen

Can anyone help me out with this?

I want to convert and re-size my "artist/album/cover.png" images to smaller "artist/album/thumb.jpg" images. The trick for me is to get all the converted images placed in the same folder as the original images..

After searching the net and trying things out, this is as far as I could get (and it doesn't work):

find . -iname '.png' | xargs -0 -i convert -resize 100x100 {} .//*/thumb.jpg

April 30th 2009 - Adam E

I use imagemagick every single day, but I can't figure out how to make it make the resized images come out as crisp and colorfull as the originals :( Here is an example of what I use:

convert -size 640x480 'filename.jpg' -resize 640x480 profile "*" -quality 100 -auto-orient 'newfilename.jpg'

The images tend to come out soft and somewhat desaturated.

Any ideas on how to fix that?

July 9th 2009 - goose

hi guys, im trying to resize a bunch of images in a folder both portrait and landscape using imagemagick. is there a way i can batch resize by the longest side? i cant use "mogrify -resize 1000 image.jpg" becuase this resizes the length and cant use "mogrify -resize x1000 image.jpg" cause that resizes the width. i need it to look at the image find the longest side and apply the resizing to that side. if anyone has any ideas i would be very grateful.

November 9th 2009 - jbova

goose:

I do similar things with images. Give me an example of two different images in a folder, including width and height. Provide the desired sizes and I'll paste an example in here.

Note that if you use an option such as resize 800x800, it will resize images as follows: one.jpg 1000x900 two.jpg 900x1000

Result: one.jpg 800x720 two.jpg 720x800

If this is what you are looking for, then you don't need to know which size is longer. If, for example, you wanted each image to have a maximal width of 800, then you can just make the height portion of the scale option an unreasonable high number for you images.

Example:

Landscape- Source: one.jpg JPEG 300x225 Command: mogrify -scale 200x1000 one.jpg Result: one.jpg JPEG 200x150

Portrait- Source: two.jpg JPEG 225x300 Command: mogrify -scale 200x1000 two.jpg Result: two.jpg JPEG 200x267

As you can see, by setting the height to a number larger than a feasible proportional height, you get all images sized to 200 pixels in width regardless of orientation.

If for some reason you still need to know the longer side, I usually use the identity function to retrieve image information first. You can do this in your shell/perl script. Let us know if you need help.

Jim

January 23rd 2010 - Mike

Tough color scheme on this website man. Hurts the eyes.

October 11th 2011 - Batch Conversion Mud

I tried the batch conversion using the for loop and alternatively spitting the commands out to file and running it as a bash script. The script would hang in mogrify, but when called directly from the shell (without a fork) it would work.

Anyone know a way around this issue ?

thanks !

September 11th 2012 - vizzy

important! in *nix do not use the 'for' loop, use 'read' and 'while' instead! or you may get into big trouble when you have files with white s p a c e s!

All content licensed under the Creative Commons License