You were never here, you didn't see anything..

Handling Images


I get pretty lazy when it comes to processing close to a hundred images for placement on a web site. First I need to make sure they are turned the right way. Then I need to make a thumbnail. Then I need a scaled down version for main display. To finish things off the thumbs need to be in the thumbs directory and the originals need to be in hi-res. Gahh that is a lot of work.

Scripting has made it simple. If one has Imagemagic and exiftran you can use this script to take care of all that for you.


#!/bin/bash

# This script will process images in the containing directory
# producing thumbnails with curved corners in a thumbs dir
# and scaled images in the base dir while moving the original
# images to a high res dir.

# ANSI Color -- use these variables to easily have different color
#    and format output. Make sure to output the reset sequence after
#    colors (f = foreground, b = background), and use the 'off'
#    feature for anything you turn on.

initializeANSI()
{
  esc=""

  blackf="${esc}[30m";   redf="${esc}[31m";    greenf="${esc}[32m"
  yellowf="${esc}[33m"   bluef="${esc}[34m";   purplef="${esc}[35m"
  cyanf="${esc}[36m";    whitef="${esc}[37m"

  blackb="${esc}[40m";   redb="${esc}[41m";    greenb="${esc}[42m"
  yellowb="${esc}[43m"   blueb="${esc}[44m";   purpleb="${esc}[45m"
  cyanb="${esc}[46m";    whiteb="${esc}[47m"

  boldon="${esc}[1m";    boldoff="${esc}[22m"
  italicson="${esc}[3m"; italicsoff="${esc}[23m"
  ulon="${esc}[4m";      uloff="${esc}[24m"
  invon="${esc}[7m";     invoff="${esc}[27m"

  reset="${esc}[0m"
}

initializeANSI
# Process any image with exif data:
echo ${boldon}"Processing files with exif data,"${reset}
for img in `ls`
	do
		exiftran -ai $img
	done

# Process jpeg files:
echo "*********************************"
echo ${boldon}"Looking for jpeg files,"${reset}
for img in `ls | grep jpeg`
	do
		mv $img `basename $img .jpeg`.jpg
		echo -e "\tRenamed $img to `basename $img .jpeg`.jpg,"
	done

echo "*********************************"
echo ${boldon}"Looking for bmp files,"${reset}
for img in `ls | grep bmp`
	do
		convert -quality 100 $img `basename $img .bmp`.jpg
		rm $img
		echo -e "\tConverted $img to `basename $img .bmp`.jpg,"
	done

# Create the corners used in rounding:
echo "*********************************"
echo ${boldon}"Creating corners,"${reset}
    convert -size 15x15 xc:black \( xc:none -draw 'circle 0,0 0,14' \) \
            -matte -compose DstOut -composite     corner_add.png
    convert -size 15x15 xc:black \( xc:none -draw 'circle 0,0 0,14' \) \
            -matte -compose DstOut -composite     corner_sub.png

# Create the  transparent square:
echo ${boldon}"Creating the transparent square,"${reset}
convert -size 120x120 xc:transparent square.gif

# Create the thumbs dir:
echo ${boldon}"Creating the thumbs dir,"${reset}
mkdir thumbs
echo "*********************************"

# Create the thumbs in thier dir:
echo ${boldon}"Creating the thumb and low-res images,"${reset}
for img in `ls *.jpg`
do

    # Create the base thumbnails:
    convert -size 400x400 $img  -thumbnail '100x100>'\
            -bordercolor black -border 0 -matte \
            \( corner_add.png -flip -flop \) -gravity NorthWest -composite \
            \( corner_add.png       -flop \) -gravity SouthWest -composite \
            \( corner_add.png -flip       \) -gravity NorthEast -composite \
            \( corner_add.png             \) -gravity SouthEast -composite \
            -compose DstOut \
            \( corner_sub.png -flip -flop \) -gravity NorthWest -composite \
            \( corner_sub.png       -flop \) -gravity SouthWest -composite \
            \( corner_sub.png -flip       \) -gravity NorthEast -composite \
            \( corner_sub.png             \) -gravity SouthEast -composite \
            -sharpen 10 thumbs/`basename $img .jpg`.gif
      echo -e "\tCreated thumbs/`basename $img .jpg`.gif"

# Create the low-res image for viewing:
convert -scale 400x400 $img `basename $img .jpg`.png
echo -e "\tCreated `basename $img .jpg`.png,"

# End the loop:
done
echo "*********************************"
# Move to thumbs dir and make them square:
 cd thumbs

 echo ${boldon}"Creating the thumbs,"${reset}
 for img in `ls *.gif`
 do

 composite -size 120x120 -gravity center $img  ../square.gif $img
 echo -e "\tCreated square thumb $img,"

done

# Change back to the root of this album:
cd ..

# Move the jpg files into thier own dir:
echo "*********************************"
echo ${boldon}"Moving .jpg files into hires,"${reset}
mkdir -p hi-res
mv *.jpg hi-res

# Clean up the mess we made:
echo ${boldon}"Cleaning up,"${reset}
rm square.gif
rm corner_add.png
rm corner_sub.png

echo ${boldon}"Done"${reset}

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This blog is kept spam free by WP-SpamFree.

Most who fail…

Most who fail have yet to really try!