Thumbnail html page of image directory

Why another one?
This builds upon files otherwise available for linux, bash, awk, bc, convert from Imagemagick and pnmfile available on this site and many places on the web with a google search on the words pnmfile and download. Imagemagick is highly recommend for convert and the other routines which come with it.

I have tried several of the free apps to do this same thing. They are several 100k, this is 2k. Their docs leave much to be desired to the point I haven't gotten any of them to work usefully. When they contain the source all I have to do is learn the author's language of choice and figure out his code.

All you have to know to change this script is bash scripting language and for most uses just a text editor to change the file name and title but that can be most easily changed after the i.html file is produced rather than changing the script. Note the method of creating the file is a trust your browser type as there is no attempt to format the layout of the images.

The script is brute force and can certainly be improved. If you do improve it think about sending me a copy.

I chose 72 dots per inch which is more or less the universal default. If you need a different size change 72 in both places in the "if fi" section.

usage
The script is copied to the directory which contains the images. Presuming you name the script file scale-make ...

./scale-make *.jpg

./scale-make *.gif
This can also be extended to series of file names and formats and other formats.
./scale-make *.jpg *.jpeg *.gif *.JPG *.JPEG *.GIF

./scale-make *.[any format extension .name]
or even
./scale-make *.*
but the latter will attempt to convert directories. Adding error reading for convert is TODO. Also convert will convert a text file giving you an entire page page sized image for even a single line of text. It is best to control the type of files you wish to convert.

As you would expect very large files can be expected to load down your machine due to the conversion to pnm. I have not found the equivalent of pnmfile for other formats or one as generic as convert for extracting the needed information.

For convenience a zip file of the script and pnmfile is included. You should find the comments in the script more than adequte to use and change it.


#!/bin/bash
# this creates a browsable web page of linked thumbnails
# it is most "impressive" on a large collections of files
# of inconsistant size

# requires linux with imagemagick and some other source 
# for pnmfile

# Matt Giwer, April, 2001, jull43@tampabay.rr.com

# make opening file with the name of choice
echo "<HTML>" > i.html
echo "<head>" >> i.html
echo "<title></title>" >> i.html
echo "</head>" >> i.html
echo "<BODY>" >> i.html

while [ $# -gt 0 ]
do

# this echo is simply to show it is working, not necessary
echo $1

# from Imagemagick read man convert
# it converts dozens of formats by looking at file identifier
# not the extension
# this can us * instead of *.jpg
# BUT clicking the thumbnail does not mean your brower can read
# the original. 
convert $1 $1.pnm

# store data on file in rr
pnmfile $1.pnm > rr

#extract width and height
w=`gawk '{ print $4 }' rr`
# echo $w
h=`gawk '{ print $6 }' rr`
# echo $h

# echo iffing

# this sets the smallest dimension equal to 72 dpi
# and scales the dimensions proportionately
# the result is easy to see for any size image although
# the resulting layout lacks the aesthetics of 
# making the largest dimenion 72 dpi

# man bc
echo "scale = 3" > rat

if [ $w -ge $h ]
then
# echo height is the smaller
# put the equation in rat
echo "1/($h/72)" >>rat
# end rat so bc won't hang
echo "quit" >> rat
#execute rat and put into ratio
echo `bc rat > ratio`

else

# echo width is the smaller
echo "1/($w/72)" >> rat
echo "quit" >> rat
echo `bc rat > ratio`

fi

# echo scaling

# dump the ratio where the scale factor goes
# pnmscale -xscale .25 -yscale .25 $1.pnm > $1.s.pnm
pnmscale -xscale `gawk '{ print $1 }' ratio`  -yscale `gawk '{ print $1 }' ratio` $1.pnm > $1.s.pnm

# echo converting

#from ImageMagick 
convert $1.s.pnm $1.s.jpg

# cleanup
rm $1.s.pnm $1.pnm 
rm rat ratio 

# create html code for thumbnails
echo "<a href=\""$1\"">""<img src=\""$1.s.jpg"\">""</a>" >> i.html

shift 

done

echo "</body></html>" >> i.html

echo done

Page reads: 6825