;;; Place this file in ~/.gimp-2.X/scripts/
;;; Run with: gimp -i -b '(add-white-background "foo.png")' -b '(gimp-quit 0)'
;;; Or: gimp -i -b '(batch-add-white-background "*.png")' -b '(gimp-quit 0)'

(define (add-white-background filename)
  (let* ((image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
         (drawable (car (gimp-image-get-active-layer image))))
    (gimp-context-set-background '(255 255 255))
    (gimp-layer-flatten drawable)
    (gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
    (gimp-image-delete image)))

(define (batch-add-white-background pattern)
  (let* ((filelist (cadr (file-glob pattern 1))))
    (while (not (null? filelist))
      (let ((filename (car filelist)))
        (add-white-background filename))
      (set! filelist (cdr filelist)))))