GIMP Script-fu

Artifact [f20cd0a61a]
Login

Artifact [f20cd0a61a]

Artifact f20cd0a61a38de356283c9c90238b6dc193c91f8:


; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.

(define (sg-batch-merge-divide directory centered)
  (let ((background "Brightfield")
        (samples '(("UV green" . "Merged B-G")
                   ("UV red"   . "Merged B-R") )))
    (let bg-loop ((filelist (cadr (file-glob (string-append directory 
                                                         DIR-SEPARATOR 
                                                         "*"
                                                         background
                                                         "*.tif" )
                                          1 ))))
      (unless (null? filelist)
        (let* ((bg-filename (car filelist))
               (bg-image (car (gimp-file-load RUN-NONINTERACTIVE
                                                 bg-filename bg-filename)))
               (bg-layer (car (gimp-image-get-active-layer bg-image))) )
          (let sample-loop ((samples samples))
            (unless (null? samples)
              (let* ((sample-parts (strbreakup bg-filename background))
                     (sample-filename (unbreakupstr sample-parts (caar samples)))
                     (sample-image (catch #f (car (gimp-file-load RUN-NONINTERACTIVE 
                                                                  sample-filename
                                                                  sample-filename ))))
                     (sample-layer (if sample-image
                                     (car (gimp-image-get-active-layer sample-image))
                                     #f )))
                (if sample-layer
                  (let ((layer (car (gimp-layer-new-from-drawable bg-layer sample-image))))
                    (gimp-image-add-layer sample-image layer 0)
                    (gimp-image-lower-layer-to-bottom sample-image layer)
                    (gimp-layer-set-mode sample-layer DIVIDE-MODE)
                    (unless (zero? centered)
                      (gimp-layer-set-offsets layer (- (/ (car (gimp-image-width sample-image)) 2)
                                                       (/ (car (gimp-drawable-width layer)) 2))
                                                    (- (/ (car (gimp-image-height sample-image)) 2) 
                                                       (/ (car (gimp-drawable-height layer)) 2)) ))
                    (set! layer (car (gimp-image-merge-down sample-image sample-layer EXPAND-AS-NECESSARY)))
                    (let ((out-filename (unbreakupstr sample-parts (cdar samples))))
                      (gimp-file-save RUN-NONINTERACTIVE sample-image layer out-filename out-filename) )
                    (gimp-image-delete sample-image) ))
                  (sample-loop (cdr samples)) )))
          (gimp-image-delete bg-image) )
          (bg-loop (cdr filelist)) ))))
      
(script-fu-register "sg-batch-merge-divide"
  "Merge TIF files per THX1138..."
  "Merge the layers from multiple files in DIVIDE mode"
  "Saul Goode"
  "Saul Goode"
  "May 2013"
  ""
  SF-DIRNAME "Directory"    ""
  SF-TOGGLE "Centered" TRUE
  )
(script-fu-menu-register "sg-batch-merge-divide"
  "<Image>/File/"
  )