0

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.)

Metalcoder
  • 2,094
  • 3
  • 24
  • 30

2 Answers2

1

The script is not coded in the way Scheme is usually written: we try real hard to avoid set! operations, what you did can be implemented using recursion instead of a while loop - in a functional programming style, that's the recommended way.

Having said that, I don't see any obvious flaw with your code, are you sure that this expression actually is false for any image?

(gimp-drawable-is-indexed image)

Try printing the above result just before the unless expression, and let's see what it shows. Start Gimp from a terminal and look at the console:

(print (gimp-drawable-is-indexed image))
(unless (gimp-drawable-is-indexed image)
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • You're right, it can be recursive. I may change it later, when a get more of the hang of Script-fu. About your question, no, I'm not sure if it is false, although it says so in the documentation. I tried A LOT to print the output of `gimp-drawable-is-indexed`, without any success. – Metalcoder May 07 '20 at 16:57
  • 1
    Try the above, use `display` (or better: a debugger if one is available) to find the value of the variables in your code, that should shine some light on where the problem is. – Óscar López May 07 '20 at 16:59
  • I'm not aware of any debugger for script-fu. I tried using `display`, but it outputs nothing. Not even something like `(display "ABC")` works. I have to use another function, `gimp-message`, but it seems to accept only strings. Running `(gimp-message (gimp-drawable-is-indexed image))` results in the following error: `Invalid type for argument 1 to gimp-message ` – Metalcoder May 07 '20 at 17:08
  • 1
    This [post](https://stackoverflow.com/q/53031322/201359) about debugging script-fu should be useful. – Óscar López May 07 '20 at 17:53
0

Use print like this:

(display (string-append "\n Indexed:" (number->string (car (gimp-drawable-is-indexed drawable))) "< \n"))
  1. You have to create a string for (display ..)

    Look first in PDB for gimp-drawable-is-indexed, returns int32 =>[(number->string] but all funcions returning lists => [(car (..]

  2. (gimp-drawable-is-indexed .) requires a layer instead of an image.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
LaciP
  • 1