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
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

Walter 2009-03-25 09:10pm - No Email - Logged IP: 65.50.0.4

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 :)

Brad Jensen 2009-04-10 05:21am - No Email - Logged IP: 173.22.213.158

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

Adam E 2009-04-30 06:49pm - No Email - Logged IP: 74.211.9.9

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?

goose 2009-07-09 06:51am - No Email - Logged IP: 195.85.232.2

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.

jbova 2009-11-09 08:45pm - No Email - Logged IP: 68.44.211.81

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

Mike 2010-01-23 01:16pm - No Email - Logged IP: 174.55.22.3

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

Batch Conversion Mud 2011-10-11 04:21am - No Email - Logged IP: 85.90.76.130

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 !

vizzy 2012-09-11 05:02am - perturb.org@... - Logged IP: 84.178.176.63

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