Search code examples
maskgimpscript-fugimpfu

GIMP Script-Fu: batch edit images with a mask


I have thousand of images that i must removed nearly 1/2 the content from.

Mannually i would:

  • load image-a
  • add alpha-channel
  • import layer image-b (pre-made mask)
  • make selection from masked out section
  • delete mask layer
  • clear selection from image a
  • export image

Here is the code i have written in an attempt to automate this:

(define  (script-fu-auto-mask-image Mask InImage OutImage)
  (let* (
      Image (car (file-tiff-load RUN-NONINTERACTIVE InImage InImage)) ImgW (car (gimp-image-width Image)) ImgH (car (gimp-image-height Image))
      PrimeLayer (car (gimp-image-get-active-layer Image))
      (gimp-layer-add-alpha PrimeLayer))
      (gimp-layer-new Image ImgW ImgH RGB-IMAGE MaskLayer 0 LAYER-MODE-NORMAL)
      (gimp-file-load-layer RUN-NONINTERACTIVE Image Mask)
      (gimp-image-set-active-layer Image MaskLayer)
      (gimp-image-select-color mask CHANNEL-OP-ADD drawable (0 0 0))
      (gimp-image-set-active-layer Image PrimeLayer)
      ;(gimp-edit-clear drawable)
      (gimp-drawable-edit-clear drawable)
      (gimp-image-remove-layer Image MaskLayer)
      (file-png-save-defaults RUN-NONINTERACTIVE Image drawable OutImage OutImage)
    )
  (gimp-displays-flush)
  (gimp-quit TRUE)
)

This is the terminal command i use to launch things and the feed back

gimp -i -c -b script-fu-auto-mask-image  "Pictures/test/mask.png" "Pictures/test/DSC00805.tif" "test.png"
bps: 16
load_contiguous
bytes_per_pixel: 6, format: 6
batch command executed successfully

It freezes then i must ctrl+c

i have tried below in the GIMP console:

(file-tiff-load RUN-INTERACTIVE "Pictures/test/DSC00805.tif" "Pictures/test/DSC00805.tif")

it returns (1) then i can see that it never actually loads the image in the status bar at the bottom of GIMPS window.

Im on Ubuntu 20.04 GIMP 2.10.18 (apt not SNAP) i tried running GIMP from a Flatpak but was getting issues with GTK modules not loading ... anyway

literally an help would be appreciated - even alternative solutions if viable


Solution

  • It's freezing because you've told gimp to start, but not told it to stop.

    you need to encapsulate your existing command and then add -b '(gimp-quit 0)'

    i.e.

    gimp -i -c -b '(script-fu-auto-mask-image  "Pictures/test/mask.png" "Pictures/test/DSC00805.tif" "test.png")' -b '(gimp-quit 0)'