#!/bin/bash #convert image.bmp image.txt #If you just one one color at a X,Y crop it first... #convert image.bmp -crop 1x1+5+6 +repage image.txt if [ -z "$(which convert 2>/dev/null)" ];then echo "Install ImageMagick and try again"; exit 1; fi set -e # directory containing the photos to include in gallery sourcephotodir=photos # the directory in to which to put the overlays images that are used to # change the border colour on mouseover overlaysdir=overlays; # delete any pre-existing thumbnails rm -rf $overlaysdir; mkdir -p $overlaysdir; rm -f *png; # seed $RANDOM with pid and seconds since epoch RANDOM=$$$(date +%s); for i in ${sourcephotodir}/*;do # rn is the number to pass to the -polaroid operation rn=$(echo $RANDOM%15 | bc); # rn will be created positive, so make it negative 50% of the time srn=$(echo $RANDOM%2 | bc); if [ $srn -eq 0 -a $rn -ne 0 ];then # let rn=0-0 doesn't work let rn=0-${rn}; fi fn=$(basename ${i}); # nfn is the filename for the thumbnail. the same name is used but with jpg # swapped to png so imagemagick creates a png image nfn=${fn/.jpg/.png}; # set height and width for the thumbnails thumbwidth=150; thumbheight=100; ow=$(identify -format %w ${i}); oh=$(identify -format %h ${i}); # figure out if the source photo has a height larger than it's width and if so # switch the thumbnail height and width around if [ ${oh} -gt ${ow} ];then foo=${thumbwidth}; thumbwidth=${thumbheight}; thumbheight=${foo}; fi # create a thumbnail, strip out any profiles etc. convert ${i} -strip -thumbnail ${thumbwidth}x${thumbheight} ${nfn}; # copy the thumbnail to the overlays dir for later processing cp ${nfn} ${overlaysdir}/ # get width and height of the thumbnail for later use w=$(identify -format %w ${nfn}); h=$(identify -format %h ${nfn}); # apply polaroid operation to the thumbnail convert $nfn -bordercolor snow -background black -polaroid $rn $nfn; #which optipng &>/dev/null && optipng -o7 -quiet $nfn; # now create the overlay image cd ${overlaysdir}; # reduce the image to one colour then negate it and extract the value color=$(convert ${nfn} -colors 1 -depth 8 -negate -format "%c" histogram:info: | sort -nr | head -1 | cut -d '#' -f 2 | cut -d ' ' -f 1); # check a value was found and use a fall back colour if not if [ -z "${color}" ];then color='#ff0000'; else color="#${color}"; fi # create a new version of the thumbnail that's 95% the size and rotateed # by the same amount the polaroid version will be for later use convert ${nfn} -matte -background none -resize 95% -rotate ${rn} foo.png; # apply polaroid operation to the thumbnail, using border colour obtained earlier convert ${nfn} -bordercolor ${color} -background transparent -polaroid ${rn} ${nfn}; # use the 95% size and rotated version of the thumbnail created earlier to # punch a transparent hole in to the thumbnail to reduce file size. g=-4; composite -compose Dst_Out -gravity center -geometry ${g}${g} foo.png ${nfn} -matte ${nfn}; rm -f foo.png #which optipng &>/dev/null && optipng -o7 -quiet $nfn; # go back to the previous working dir to process the next image cd - >/dev/null; done # because if you've re-generated all the images then the web page needs to be # regenerated so it contains the correct width/height attributes for all the img # elements ./makehtml