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

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:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 16 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
16 comments… add one
  • riscphree Dec 5, 2005 @ 3:16

    Wow, that is awesome!

    But why cant you put the perl script here?

  • LinuxTitli Dec 5, 2005 @ 11:33

    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 Dec 6, 2005 @ 5:55

    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!


  • LinuxTitli Dec 6, 2005 @ 13:20

    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 Nov 14, 2006 @ 6:50

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

  • michuk May 11, 2007 @ 8:53

    Another useful article on the subject:
    (massive resizing, adding text or frames to images, etc, all with ImageMagick)

  • Felix Jul 13, 2007 @ 19:10

    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 Jul 13, 2007 @ 20:27

    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;

  • Steve Lajoie Aug 11, 2007 @ 0:36

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

  • Flor Sep 6, 2007 @ 2:30

    I have a suggestion to execute it in bash:

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

  • marc41 Mar 26, 2009 @ 14:58

    Here is a script that does this and MUCH MORE!

    — Enjoy!

  • lee Apr 15, 2011 @ 14:16

    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 Feb 22, 2012 @ 17:12

    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 Nov 29, 2012 @ 12:04

    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

  • Allan Mar 14, 2016 @ 19:12

    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 🙂

  • Texbrew Nov 21, 2016 @ 21:52

    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.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum