Howto: Linux Creating a Image Thumbnails from shell prompt

last updated in Categories Linux, Linux desktop, Tips

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

To create a thumbnail of the abc.png image with 200px height, you need to type:
$ convert -thumbnail x200 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:

for i in $FILES
echo "Prcoessing image $i ..."
/usr/bin/convert -thumbnail 200 $i thumb.$i

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:


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

16 comment

  1. 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)

  2. 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
    convert -scale ’48×38′ $i TN_$i
    echo $i processed

    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
    djpeg $i | pnmscale -xysize 48 38 | cjpeg -opti -progr -qual ‘75%’ > TN_$i
    echo $i processed

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


  3. 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.

  4. 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

  5. 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
    djpeg $i | pnmscale -xysize 48 38 | cjpeg -optimize -progressiv -quality 75 > TN_$i;
    echo $i thumbnails created;

  6. 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 $@
    echo “found file $image and making thumb”
    convert $image -resize 100x thumb-$image

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

  7. 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….

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

    Thanks Alejandro G. Bedoya November 14, 2006

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

  10. Most of these are scripts, not shell commands. Entirely pointless and irrelevant to go through the hassle of opening a text editor, then chmoding a script, for what is actually a oneliner.
    $ for i in `ls *.jpg`; do convert -thumbnail 190 $i thumb.$i; done

    Might need a little modification if your filenames have spaces in them, bit thats easy to figure out 🙂

  11. Thank you. I just began using Manjaro linux and I’m am still unfamiliar with its software. Looking for a way to create thumbnails, I found this tutorial and can verify it works with .jpg files.

    I just converted a 309kb file to 2.2kb , just what I needed.

    Have a question? Post it on our forum!