Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have found this tool that optimizes images for the web. What is surprising for me (of course, there will be other tools like this :)) is that it only optimizes the uploaded image if it is possible, I mean, if it considers that the image is already optimized for the web it says "No savings".

My question: before running something like convert -quality 80, is there any way (using ImageMagick or any other tool in Linux) to ckeck if an image is already optimized for the web? In that case I would not run convert -quality 80.

share|improve this question
1  
Looking at the brief delay between uploading and getting the verdict, as compared to no delay between getting the verdict and the download link, I think they just compress the image and see if it comes out smaller. –  Ulrich Schwarz Jul 8 '13 at 21:06
add comment

2 Answers 2

up vote 2 down vote accepted

Based on your reference to the ImageMagick convert method and quality parameter, it appears you're working with JPEG images. If that's the case, the EXIF information for JPEG images does not have a standard compression level tag.

However, ImageMagick appears to be able to obtain this information from the image's quantization tables using the identify command. So try this:

identify -verbose yourimage.jpg

And then you should be able to parse out the quality level from the standard output. For example:

...
Compression: JPEG
Quality: 80
...
share|improve this answer
add comment

I would just crush all of your images using a script like the one below which I use. Be sure to back up the images directory first. If any resultant images are smaller than the ones you were serving, then they weren't optimised!

#!/bin/sh

# script for optimizing images in a directory (recursive)
# pngcrush & jpegtran settings from:
# http://developer.yahoo.com/performance/rules.html#opt_images

# pngcrush
for png in `find $1 -iname "*.png"`; do
    echo "crushing $png ..."
    pngcrush -rem alla -reduce -brute "$png" temp.png

    # preserve original on error
    if [ $? = 0 ]; then
        mv -f temp.png "$png"
    else
        rm temp.png
    fi
done

# jpegtran
for jpg in `find $1 -iname "*.jpg"`; do
    echo "crushing $jpg ..."
    jpegtran -copy none -optimize "$jpg" > temp.jpg

    # preserve original on error
    if [ $? = 0 ]; then
        mv -f temp.jpg "$jpg"
    else
        rm temp.jpg
    fi
done

pngcrush comes from here: http://pmt.sourceforge.net/pngcrush/
jpegtran comes from here: http://jpegclub.org/jpegtran/

Three notes:

  1. pngout has better compression on most files: http://advsys.net/ken/utils.htm [source]
  2. For JPEGs, spit them out in various quality settings and see what the lowest acceptable quality value is (it will vary by image and destination screen size)
  3. cwebp can probably do better on both jpegs and PNGs, but can only be used in Chrome and Opera (soon Firefox): https://developers.google.com/speed/webp/
share|improve this answer
    
thanks, would you find interesting the option -perfect for jpegtran? –  tirengarfio Jul 9 '13 at 12:52
    
I left that out in case your build of jpegtran doesn't support it (I know that on GoDaddy shared hosts, for example, the version of jpegtran there dates from 1998 and thus doesn't support it). -perfect is primarily of use in the rotation operations where you have edges that don't fall on block boundaries. –  Nicholas Jul 9 '13 at 12:59
    
You may also be interested in the lossless crop'n'drop feature of the most recent builds: stackoverflow.com/a/17187580/760706 –  Nicholas Jul 9 '13 at 13:01
    
I have tried jpegtran -copy none -optimize "$jpg" > temp.jpg but it outputs a image more heavier than smushit.com/ysmush.it It is supposed that ysmush is also using jpegtran: developer.yahoo.com/yslow/smushit/faq.html#faq_crushtool –  tirengarfio Jul 9 '13 at 13:06
    
@tirengarfio try also using -progressive, but it could be that smushit is just lowering the image quality (-q 60 or something) so what you get out is bound to be smaller if you do that. –  Nicholas Jul 9 '13 at 13:49
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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