ImageMagick allows image manipulations from the command line. Using this in a shell script should do what you want.

Description: image manipulation programs
ImageMagick is a software suite to...