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:
- ImageMagick Studio software website.
- convert man page
You may also be interested in other helpful articles:
- Tutorial: Working with UNIX and Linux Shell
- How Do I Burn MP3 onto an audio CD from Linux shell command prompt?
- nixCraft FAQ Roundup
- nixCraft FAQ Roundup
- Linux creating CD-ROM ISO image
Discussion on This Article:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!


Wow, that is awesome!
But why cant you put the perl script here?
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)
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
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.
find . -name \*.jpg -exec convert {} -resize 25% th_{} \;
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)
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
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
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”.
I have a suggestion to execute it in bash:
ls *.png | xargs -I {} convert -thumbnail 200 {} thumb.{}