User:Inductiveload/Remove-background-colour.scm

; "Remove Paper Texture" is a script for The GIMP
;
; Removes paper texture from scanned images (using the fill tool)
;   Should work on coloured images
;
; The script is located in menu "<Image> / Filters / Enhance"
; Last changed: 1 September 2010
;
; 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 3 of the License, or
; (at your option) any later version.

(define (add-background-layer
    theImage
    layerName
    fillType
    )

    (let (
            ;Get the number of layers
            (numLayers (car (gimp-image-get-layers theImage)))

            ;Make a new layer
            (bgLayer (car (gimp-layer-new theImage
                1 1 RGB-IMAGE layerName 100 NORMAL-MODE )))
        )

        ;Add to the image at the lowest postion
        (gimp-image-add-layer theImage bgLayer numLayers )

        ;Resize to fit the image
        (gimp-layer-resize-to-image-size bgLayer)

        ;Fill it with pure white
        (gimp-drawable-fill bgLayer fillType)
    )
)

(define (script-fu-remove-background-colour
    theImage
    theLayer
)

    ;Start an undo group so the process can be undone with one undo
    (gimp-image-undo-group-start theImage)

    ; Create a new background layer and fill white
    (add-background-layer theImage "Background" WHITE-FILL)

    ; add alpha channel to the image
    (gimp-layer-add-alpha theLayer)

    ;Select everything
    (gimp-selection-all theImage)

    ;Apply the color erasing fill
    (gimp-edit-bucket-fill theLayer FG-BUCKET-FILL COLOR-ERASE-MODE 100 0 0 0 0)

    ;Desaturate
    (gimp-desaturate theLayer)

    ;Apply an unsharp mask
    ;(plug-in-unsharp-mask TRUE theImage theLayer 5 0.25 0)
    
    ;Merge layers
    (gimp-image-merge-visible-layers theImage CLIP-TO-IMAGE)
    
    (set! theLayer (car (gimp-image-get-active-layer theImage)))
    (gimp-layer-set-name theLayer "Image")
    
    ;Finish the undo group for the process
    (gimp-image-undo-group-end theImage)

    ;Ensure the updated image is displayed now
    (gimp-displays-flush)

)

;Register the script w/ GIMP.
(script-fu-register
        "script-fu-remove-background-colour"       ;func name
        "Remove background colour"               ;menu label
        "Removes a flat background colour from an image and desaturates";description
        "Inductiveload"                             ;author
        "GPL"                                       ;copyright notice
        "Aug. 2010"                                 ;date created
        ""                                          ;image type that the script works on

          SF-IMAGE    "Image"         0
          SF-DRAWABLE "Layer to convert" 0

)

(script-fu-menu-register "script-fu-remove-background-colour"
    "<Image>/Filters/Enhance")