≡ Menu

Howto: Linux Creating a Image Thumbnails from shell prompt

Recently I had got strange request from our web designing developers they want me to create thumbnails for all images (around 10000+ images). First I thought it will be easy, I went through software such as Gimp and then searched online etc.
Finally friend of mine who is a graphics artist and uses Linux actively pointed out me wonderful utility called create which is part of ImageMagick Studio software. ImageMagick is a free software suite to create, edit, and compose images. It can read, convert and write images in a large variety of formats. Images can be cropped, colors can be changed, various effects can be applied, images can be rotated and combined, and text, lines, polygons, ellipses and Bezier curves can be added to images and stretched and rotated. And guess what, it can be use from shell prompt and you can write shell/perl scripts too.

Install imagemagick, use apt-get command as follows:
# apt-get install imagemagick

You need to pass -thumbnail argument to convert command:
convert thumbnail <width>x<height> image.png thumbnail.png

So to create a thumbnail of the abc.png image with 200px width, you need to type:
$ convert -thumbnail 200 abc.png thumb.abc.png

To create a thumbnail of the abc.png image with 200px height, you need to type:
$ convert -thumbnail x200 abc.png thumb.abc.png

But, hold on real part is ahead, since I had to go through 10000+ images I wrote perl script (although I can not put real script here because of legal issues). Nevertheless here is sample shell script:

#!/bin/bash
FILES="$@"
for i in $FILES
do
echo "Prcoessing image $i ..."
/usr/bin/convert -thumbnail 200 $i thumb.$i
done

Update: Tim (below in comment) pointed out another method and script, to create thumbnails for jpg images with optimization in mind.For more information see:

Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

Comments on this entry are closed.

  • riscphree December 5, 2005, 3:16 am

    Wow, that is awesome!

    But why cant you put the perl script here?

  • LinuxTitli December 5, 2005, 11:33 am

    Well I have an agreement with my employer, the script is part of in house build software system, I wrote basic script and later they modified it for web based software, so I can’t put it on line until and unless they give me permission (copyright sucks)

  • Tim December 6, 2005, 5:55 am

    Two comments here:

    a) for batch purposes, you can either use a shell script or even the mogrify command (part of imagemagick):

    bash$ for i in *.jpg
    do
    convert -scale ’48×38′ $i TN_$i
    echo $i processed
    done

    mogrify -format jpg -quality ‘95%’ *.tiff (bulk-conversion)

    b) For thumbnails, you quite often don’t want to use imagemagick or convert or whatever; for my normal size (48×38), it creates a whopping huge 50K jpg when there are other ways of getting them much much smaller:

    bash$ for i in *.jpg
    do
    djpeg $i | pnmscale -xysize 48 38 | cjpeg -opti -progr -qual ‘75%’ > TN_$i
    echo $i processed
    done

    This keeps the average image-size down to about 1K – rather more suited to a page of thumbnails!

    HTH

  • LinuxTitli December 6, 2005, 1:20 pm

    You are right tim. But main problem is we got all images in .png format and not .jpg format. However if it is in jpg format your script will save disk space and it is really optimized solution. Thanks for sharing with us, appreciate your post.

  • Alejandro G. Bedoya November 14, 2006, 6:50 am

    find . -name \*.jpg -exec convert {} -resize 25% th_{} \;

  • michuk May 11, 2007, 8:53 am

    Another useful article on the subject: http://polishlinux.org/apps/graphics/enchanting-pictures-with-imagemagick/
    (massive resizing, adding text or frames to images, etc, all with ImageMagick)

  • Felix July 13, 2007, 7:10 pm

    Addition to Tim’s script:
    If you do not want to re-thumb the thumbnails which would result in things like TN_TN_image.jpg, simply use this first line:

    for i in [^TN_]*.jpg

  • Felix July 13, 2007, 8:27 pm

    The script is buggy with my versions of djpeg (6b), pnmscale (netpbm 10.0) and cjpeg (6b). Not processing the thumbs that are already there, it should read:

    for i in [^TN_]*.jpg
    do
    djpeg $i | pnmscale -xysize 48 38 | cjpeg -optimize -progressiv -quality 75 > TN_$i;
    echo $i thumbnails created;
    done

  • Steve Lajoie August 11, 2007, 12:36 am

    The latests version of convert doesn’t seem to have the -thumbnail option.

    My version of the shell script works likes this:

    for image in $@
    do
    echo “found file $image and making thumb”
    convert $image -resize 100x thumb-$image
    done

    So if you have a image named “image001.jpg” you get a thumbnail named “thumb-image001.jpg”.

  • Flor September 6, 2007, 2:30 am

    I have a suggestion to execute it in bash:

    ls *.png | xargs -I {} convert -thumbnail 200 {} thumb.{}

  • marc41 March 26, 2009, 2:58 pm

    Here is a script that does this and MUCH MORE!

    — Enjoy!

  • lee April 15, 2011, 2:16 pm

    You can get tiny JPEGs or PNGs out of large files, using IM, if you RTFM rather than blast it — check out colour profiles, the ‘thumbnail’ command option, colour spaces….

  • Khanh Vietnam February 22, 2012, 5:12 pm

    My shell for all sub-folder: find . -name \*.jpg -exec convert {} -size 70×50 {}_thumb.jpg \;

    Thanks Alejandro G. Bedoya November 14, 2006

  • kanchan sharma November 29, 2012, 12:04 pm

    #!/bin/bash
    FILES=* #### This change in code is helpful for us.
    for i in $FILES
    do
    echo “Prcoessing image $i …”
    /usr/bin/convert -thumbnail 200 $i thumb.$i
    done