21

I have a camera which produces photographs of 3008x2000 pixels. I use Image Magick to scale and resize the photos to be put up on my website. The size of the images I am using on the website is 602x400. I use this command to reduce the size:

convert DSC_0124.JPG -scale 20% -size 24% img1.jpg

This produces an image which is 602x400 pixels in size. But the file size will be always above 250KB. More images on a single html page means the page will be heavier and loading time will be longer. Are there any features in image magic that will help me to keep the file size as small as possible, possibly, below 100KB. But the image size should be the same, that is, 602x400px. I have achieved similar optimisation with SEAMonster tool for MS Windows. As it doean't have a commandline alternative, it wouldn't be of much help when there are hundreds of images to be converted.

4 Answers 4

36

Use command as Delan proposed with additional "-strip" flag to remove EXIF data, this have reduced the size of some of my images drastically. Here is a bash script for unix platforms, but you can use the second part only for individual images.

for X in *.jpg; do convert "$X" -resize 602x400 -strip -quality 86 "$X"; done

This will convert all images in the directory.

1
  • 8
    Warning: using convert with the same input and output file name may be an unsafe operation. Either write to a temporary file name and conditionally mv the temp overwriting the original, or just use mogrify (part of the ImageMagick suite). The man page for mogrify(1) states "Mogrify overwrites the original image file, whereas, convert(1) writes to a different image file." Dec 14, 2014 at 10:53
10

Use -quality to set the compression level:

convert DSC_0124.JPG -scale 20% -size 24% -quality [0..100] img1.jpg
4

You can define the maximum size of the output image at 100KB like this:

convert DSC_0124.JPG -resize 602x400! -strip -define jpeg:extent=100KB img1.jpg
1
  • 3
    Be careful, the "!" flag to resize ignores aspect ratio and ! may be a special shell character, which typically needs to be escaped. Dec 14, 2014 at 10:40
3

If you are running your website on PHP, you might want to consider the SLIR image resizing script, it does a great job resizing to various constraints (see below) and caches the results.

Parameters:
w    Maximum width
h    Maximum height
c    Crop ratio
q    Quality
b    Background fill color
p    Progressive

http://shiftingpixel.com/2008/03/03/smart-image-resizer/

http://code.google.com/p/smart-lencioni-image-resizer/

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.