Advertisement
Syniurge

Old book illustration cleanup script

Oct 2nd, 2013
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 4.64 KB | None | 0 0
  1. ;; Place the script in ~/.gimp/scripts, then invoke with:
  2. ;; gimp -i -b '(auto-figuier <input file> <output file>)' -b ‘(gimp-quit 0)’
  3.  
  4. (define (figuier
  5.    image
  6.    drawable
  7.    )
  8.  
  9.    (let* (
  10.           (edge-layer 0)
  11.           (nonedge-selection -1)
  12.       )
  13.  
  14.      (let (
  15.           (numlayers (car (gimp-image-get-layers image)))
  16.           (bglayer (car (gimp-layer-new image
  17.               1 1 RGB-IMAGE "White background" 100 NORMAL-MODE)))
  18.        )
  19.  
  20.        (gimp-image-add-layer image bglayer numlayers )
  21.        (gimp-layer-resize-to-image-size bglayer)
  22.  
  23.        (gimp-drawable-fill bglayer WHITE-FILL)
  24.      )
  25.  
  26.      (gimp-image-set-active-layer image drawable)
  27.      (gimp-layer-add-alpha drawable)
  28.  
  29.      ; Selective Gauss filter to blur the background only
  30.      (plug-in-sel-gauss RUN-NONINTERACTIVE image drawable 10 10)
  31.  
  32.      ; Setup a selection mask to preserve light edges
  33.      (set! edge-layer (car (gimp-layer-copy drawable FALSE)))
  34.      (gimp-image-add-layer image edge-layer -1)
  35.      (gimp-layer-set-name edge-layer "Edges")
  36.      (gimp-image-lower-layer image edge-layer) (gimp-image-lower-layer image edge-layer)
  37.      (gimp-layer-flatten edge-layer)
  38.      (plug-in-edge RUN-NONINTERACTIVE image edge-layer 1 2 4)
  39.      (gimp-image-set-active-layer image drawable)
  40.      (gimp-by-color-select-full edge-layer '(0 0 0) 8 0 TRUE TRUE 2 2 FALSE FALSE SELECT-CRITERION-COMPOSITE)
  41.      (gimp-by-color-select-full edge-layer '(4 6 16) 20 CHANNEL-OP-ADD TRUE FALSE 0 0 FALSE FALSE SELECT-CRITERION-COMPOSITE)
  42.      (gimp-selection-grow image 1)
  43.      (set! nonedge-selection (car (gimp-selection-save image)))
  44.  
  45.      ; Brighten reddish marks to have them removed with the background later
  46.      (gimp-image-set-active-layer image drawable)
  47.      (gimp-by-color-select-full drawable '(230 200 150) 22 CHANNEL-OP-REPLACE FALSE TRUE 2 2 FALSE FALSE SELECT-CRITERION-COMPOSITE)
  48.      (gimp-by-color-select-full drawable '(216 177 104) 30 CHANNEL-OP-ADD FALSE TRUE 2 2 FALSE FALSE SELECT-CRITERION-COMPOSITE)
  49.      (gimp-by-color-select-full drawable '(182 158 98) 3 CHANNEL-OP-ADD FALSE TRUE 2 2 FALSE FALSE SELECT-CRITERION-COMPOSITE)
  50.      (gimp-by-color-select-full drawable '(221 197 134) 3 CHANNEL-OP-ADD FALSE TRUE 2 2 FALSE FALSE SELECT-CRITERION-COMPOSITE)
  51.      (gimp-image-select-item image CHANNEL-OP-INTERSECT nonedge-selection)
  52.      (gimp-brightness-contrast drawable 100 0)
  53.  
  54.      ; Sharpening filter
  55.      (gimp-selection-none image)
  56.      (plug-in-unsharp-mask RUN-NONINTERACTIVE image drawable 5 0.20 20)
  57.      (gimp-brightness-contrast drawable 40 15)
  58.  
  59.     ; Attempt to remove darker characters impressed from the facing page
  60.      (gimp-by-color-select-full drawable '(236 226 186) 12 CHANNEL-OP-REPLACE TRUE TRUE 6 6 FALSE FALSE SELECT-CRITERION-COMPOSITE)
  61.      (gimp-image-select-item image CHANNEL-OP-INTERSECT nonedge-selection)
  62.      (gimp-edit-clear drawable)
  63.  
  64.      ; Remove the old book background
  65.      (for-each (lambda (x)
  66.          (gimp-by-color-select-full drawable x 12 CHANNEL-OP-REPLACE TRUE TRUE 4 4 FALSE FALSE SELECT-CRITERION-COMPOSITE)
  67.          (gimp-image-select-item image CHANNEL-OP-INTERSECT nonedge-selection)
  68.          (gimp-edit-clear drawable))
  69.           (list '(252 242 203) '(255 246 223) '(255 255 241)) ; List of colors to look for, may need tweaking
  70.      )
  71.  
  72.      (gimp-image-remove-layer image edge-layer)
  73.      (gimp-image-set-active-layer image drawable)
  74.  
  75.      (gimp-selection-none image)
  76.      (gimp-desaturate drawable)
  77.      (gimp-image-convert-grayscale image)
  78.  
  79.      (gimp-image-flatten image)
  80.  
  81.      (gimp-displays-flush)
  82.   )
  83. )
  84.  
  85. (define (auto-figuier
  86.    input
  87.    output
  88.    )
  89.  
  90.    (let* (
  91.           (image (car (gimp-file-load RUN-NONINTERACTIVE input input)))
  92.           (drawable (car (gimp-image-get-active-layer image)))
  93.       )
  94.  
  95.      (figuier image drawable)
  96.  
  97.      (gimp-file-save RUN-NONINTERACTIVE image (car (gimp-image-get-active-layer image)) output output)
  98.      (gimp-image-delete image)
  99.   )
  100. )
  101.  
  102. (gimp-plugin-menu-branch-register "<Image>/Filters" "Digitization")
  103. (script-fu-register "figuier"
  104.                     "Old book illustration cleanup"
  105.                     "Automatically make illustrations from old books e-book ready by erasing the sepia background, removing ink marks from the facing page and reddish spots, and then by sharpenning and desaturating the final picture."
  106.                     "Elie Morisse"
  107.                     "contributed by Elie Morisse"
  108.                     "2013"
  109.                     "RGB*"
  110.                     SF-IMAGE "Image" 0
  111.                     SF-DRAWABLE "Layer" 0)
  112. (script-fu-menu-register "figuier"
  113.                          "<Image>/Filters/Digitization")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement