The following script is meant to be invoked via Gimp CLI interface, and it changes the color modes of all PNGs in the current directory to indexed:
(define (script-fu-batch-indexify pattern)
(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-image-convert-indexed image NO-DITHER MAKE-PALETTE 256 0 0 "")
(gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
(gimp-image-delete image))
(set! filelist (cdr filelist)))))
This script works well, but it requires that each PNG in the currently directory isn't indexed. It will abort immediately when it finds a indexed PNG. My intention is to make it skip indexed PNGs. I believe the best way to do so is like this:
(define (script-fu-batch-indexify pattern num-cols)
(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))))
(unless (gimp-drawable-is-indexed image) ; Now it does not work anymore...
(gimp-image-convert-indexed image NO-DITHER MAKE-PALETTE num-cols 0 0 "")
(gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
(gimp-image-delete image)))
(set! filelist (cdr filelist)))))
But doing so prevents the script from working! It executes without any error, and without indexing any non-indexed PNGs. I tried using if
, with the same results. So, what am I getting wrong here?
(I'm using Gimp 2.8.22 over Linux Mint 19.3.)