Script-fu de GIMP para automatizar

Elemhunter / Habanero Scans / Deadscanlations es un blog que sube escaneos de manga, con muy buena definición. Había uno que me interesaba, pero papel se veía demasiado gris, supongo que por ser reciclado o de mala calidad.
Con GIMP pude mejorar el contraste, cambiando los niveles primero y después sustituyendo el color del fondo por blanco. Quería automatizar ese proceso para todas las imágenes.
Me quedó este script (TinyScheme):
(define (filename-basename orig-name)
(car (strbreakup orig-name "."))
; Nimmzo 09/09/30: the string split function strbreakup is defined
; in the compatibility file from SIOD to TinyScheme:
; C:\Program Files\GIMP\share\gimp\2.0\scripts\script-fu-compat.init
) ; end filename-basename
(define (batch-levels pattern
low-input
high-input
paper-red
paper-green
paper-blue
)
(let* ((filelist (cadr (file-glob pattern 1))))
(while (not (null? filelist))
(let* ((filename (car filelist))
(image (car (gimp-file-load RUN-NONINTERACTIVE
filename filename)))
(drawable (car (gimp-image-get-active-layer image))))
(gimp-levels
drawable 0 low-input high-input 1.0 0 255)
(plug-in-exchange RUN-NONINTERACTIVE
image drawable paper-red paper-green paper-blue 255 255 255 255 255 255)
(gimp-file-save RUN-NONINTERACTIVE
image drawable (string-append (filename-basename filename) "_mod.tif") filename)
(gimp-image-delete image))
(set! filelist (cdr filelist)))))
low-input
y high-input
es el rango de niveles que queremos «expandir» a 0-255, y paper-{red, green, blue} el RGB del fondo grisáceo. Con esto último cuidado porque en la interfaz gráfica al seleccionar el color se puede hacer de 0-100 o de 0-255. Para este script necesitamos 0-255:

Para ejecutarlo:
gimp -i -b '(batch-levels "*.tif" 126 236 239 239 239)' -b '(gimp-quit 0)'
Otra tarea que he necesitado automatizar es el escaneo de un libro. Lo hice desde un mismo lado y girando 180 grados en cada cambio de página. De este modo, el lomo queda más cerca de las manos y es más cómodo al aplastarlo contra el escáner.

Necesitamos girar 180 grados las páginas a la izquierda (en mi caso las pares), y luego cortar:
(define (filename-basename orig-name)
(car (strbreakup orig-name "."))
; Nimmzo 09/09/30: the string split function strbreakup is defined
; in the compatibility file from SIOD to TinyScheme:
; C:\Program Files\GIMP\share\gimp\2.0\scripts\script-fu-compat.init
) ; end filename-basename
(define (my-even? x)
(= (remainder x 2) 0)
)
(define (boolean-to-string val) (if val "#t" "#f"))
(define (crop pattern
cropped-width
scaled-height)
(let* ((filelist (cadr (file-glob pattern 1))))
(while (not (null? filelist))
(let* ((filename (car filelist))
(image (car (gimp-file-load RUN-NONINTERACTIVE
filename filename)))
(drawable (car (gimp-image-get-active-layer image)))
(width (car (gimp-image-width image)) )
(height (car (gimp-image-height image)) )
(basename (filename-basename filename))
(page-number (string->number basename) )
(LZW 1)
)
;(gimp-message (boolean-to-string page-number))
(gimp-message filename)
(gimp-image-crop image cropped-width height (- width cropped-width) 0)
;(gimp-message (boolean-to-string (my-even? page-number)))
(if (my-even? page-number)
(begin
;(gimp-message "PAR")
(gimp-image-rotate image ROTATE-180)
)
)
(gimp-drawable-transform-scale drawable 0 0 (* (/ cropped-width height) scaled-height) scaled-height 0 1 0 3 0)
(gimp-file-save RUN-NONINTERACTIVE
image drawable (string-append (filename-basename filename) "_mod.png") filename)
;(file-tiff-save RUN-NONINTERACTIVE
; image drawable (string-append (filename-basename filename) "_mod.tif") filename LZW)
(gimp-image-delete image))
(set! filelist (cdr filelist))))
)
Nótese especialmente el begin dentro del if. De esta manera se devuelve la a última línea, gimp-image-rotate. Sin begin no funciona.
Ejecutamos con:
gimp -i -c -b '(crop "*.tiff" 4316 2200)' -b '(gimp-quit 0)'