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/{} |
Replies
find . -name "*.jpg" -exec convert -quality 75 {} /tmp/output/{} \;
achieves the same thing
find . -iname "*.jpg" | perl -e 'for(<>) { chomp $_;@a=split(/\//,$_);$str = "convert -resize 50% -quality 80 $_ thumbnails/".$a[-1];print $str."\n";`$str`}'
anyone knows that using xargs is much more effecient than using the -exec find flag.
also you can do:
for i in `ls *.jpg`; do convert -resize 50% -quality 80 $1 conv_$1; done
> 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
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.
Surely you can just just do this:
convert * -resize 30x30 -quality 100 scaled.jpg
> > 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.
To answer a previous question, you can resize images on the fly using phpThumb. I works very well.
www.munkeefarm.com
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