Resizing some images to same width

May 2010


For a slideshow on a webpage, we want to resize all the images to the same width. (Ideally they should also have the same height, but we don't want to alter the aspect ratio.)

  1. Download all the images to some directory

    In this case, they were on a webpage already, and I downloaded them from there. There were only six, so no need for fancy wget scripts...

  2. Figuring out the minimum width

    Enlarging pictures is tricky with jpeg, so we want to reduce all the images to the width of the smallest.

    From this output, it is easy to figure out the smallest width (418). For longer lists of files, we could grep -o ^[0-9]* or awk -Fx '{print $1"' the first number out and pipe the list through sort and head...

  3. Reducing the pictures

    We use this four-line script:

    for F in $(ls -1) ; do
    X=$(identify -format "%g" ${F}|cut -d 'x' -f 1)
    convert -resize $((41800 / ${X}))% ${F} ${F}.converted
    mv ${F}.converted ${F}
    done
    	

    ... and we list the output: