Using Gimp's script-fu to convert images into a different format

JOB The following script shows, how Gimp's script_fu language extension can be used to drive Gimp in batch mode to convert images.

#!/usr/bin/ksh
# --------------------------------------------------------------------
# Usage:
# Convert PNG Images to gif using gimp and it's script-fu capabilities
# --------------------------------------------------------------------
# Revision History:
# 18.08.05, Johann Oberdorfer, Initial Release.
#           [email protected]
# --------------------------------------------------------------------
# See also:
# the following script was found in a tip, but won't work directly...
# (define (script-fu-png2gif  num-colors infile outfile)
#   (let* ((img (car (file-png-load 1 "bgr.png" "bgr.png")))
#          (drawable (car (gimp-image-active-drawable img))))
#         (gimp-convert-indexed img FS-DITHER MAKE-PALETTE 64 FALSE FALSE "")
#         (file-gif-save 1 img drawable "bgr.gif" "bgr.gif" TRUE  0 0 0)))
# 
# (script-fu-register "script-fu-png2gif"
# _"<Toolbox>/Xtns/Script-Fu/Utils/conv"
# "CONV"
# "Johann"
# "Johann"
# "2005-08-18"
# ""
# SF-VALUE "num-colors" "64"
# SF-FILENAME "Infile"  "test.png"
# SF-FILENAME "Outfile" "test.gif"
# )
# call gimp in batch mode:
#  gimp -c -i -d -b '(script-fu-png2gif 64 "plan.png" "plan.gif")' \
#  '(gimp-quit 0)'

# --------------------------------------------------------------------
# --------------------------------------------------------------------


source_fmt=png
target_fmt=gif

integer maxcnt=`ls *.${source_fmt} | wc -l`
integer cnt=1

for i in `ls *.${source_fmt}`
do
  echo "\nConverting image ($cnt/$maxcnt): $i ..."

  inp_file=$i

  # replace extension ...
  name=`echo $i | awk '{ n=split($0,a,"."); print a[1]; }'`
  out_file="${name}.${target_fmt}"

  # convert PNG images using gimp...
  # --------------------------------
  gimp_script="(let* ((img (car (file-png-load 1 \"$inp_file\" \"$inp_file\"))) \n
          (drawable (car (gimp-image-active-drawable img)))) \n
          (gimp-convert-indexed img FS-DITHER MAKE-PALETTE 64 FALSE FALSE \"\") \n
          (file-gif-save 1 img drawable \"$out_file\" \"$out_file\" TRUE  0 0 0) \n
          (gimp-quit 0))"

  echo ""
  echo $gimp_script
  echo ""

  gimp -c -i -d -b "$gimp_script"
  # --------------------------------
  
  ((cnt = cnt + 1))
done