2019-09-23 Batch white to transparency using Gimp

I have a directory with 1350 PNG images with an indexed palette and a white background and I want to turn white into a transparent background like Gimp does: a light blue turns into a semi-transparent blue. That’s the idea.

So I read up on scripting a bit, I used the *Help* → *Procedure Browser*, and I wrote a little scheme function based on what I found online.

This is `~/.config/GIMP/2.10/scripts/white-to-transparent.scm`:

(define (batch-white-to-transparent 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-layer-add-alpha drawable)
	     (plug-in-colortoalpha RUN-NONINTERACTIVE
                                   image drawable "white")
             (gimp-file-save RUN-NONINTERACTIVE
                             image drawable filename filename)
             (gimp-image-delete image))
           (set! filelist (cdr filelist)))))

And I invoke Gimp as follows in the directory where all the PNG files are:

gimp -i -b '(batch-white-to-transparent "*.png")' -b '(gimp-quit 0)'

It’s taking quite a while: about 1min/30 images, so I’m expecting 45min for all of them. Time for bed while this runs!

​#Gimp

Comments

(Please contact me if you want to remove your comment.)

I am sure ImageMagick would be the faster (and much easier to script) solution here:

convert image.png -transparent white result.png

– Andreas Gohr 2019-09-24 04:44 UTC

Andreas Gohr

---

In this case that isn’t true because I don’t just want to make white pixels transparent. I also want to make light blue pixels semi-transparent (these arise due to anti-aliasing of the original scan). The answer given on StackOverflow doesn’t look simple at all:

answer given on StackOverflow

convert                        \
    original.png               \
    \(                         \
       -clone 0                \
       -fill "#a0132e"         \
       -colorize 100           \
    \)                         \
    \(                         \
       -clone 0,1              \
       -compose difference     \
       -composite              \
       -separate               \
       +channel                \
       -evaluate-sequence max  \
       -auto-level             \
    \)                         \
     -delete 1                 \
     -alpha off                \
     -compose over             \
     -compose copy_opacity     \
     -composite                \
    output.png

I don’t know whether it’d be faster, of course... 🙂

– Alex Schroeder 2019-09-24 20:27 UTC

---

@Miredly said:

@Miredly

Wow, that is a hairy answer.
Generally, `convert <imagefile> -fuzz XX% -transparent <color> <resultfile>` works for me

When I said the same thing regarding partial transparency, Miredly said:

that’s what `-fuzz XX%` is for, the higher percentage, the larger the gradient of that color is effected

Maybe next time!

– Alex Schroeder 2019-09-25 05:13 UTC

---

Gimp’s (tag(changethename) btw) color to alpha works much better for this particular purpose than Image Magick’s

– Sandra 2020-12-04 18:48 UTC

Sandra

---

@estebanm wrote GIMP Scripting.

@estebanm

GIMP Scripting

– 2020-12-04 18:51 UTC

---

Hello, I tried to execute your script but nothing happens and I just get 2 times the output “batch command executed successfully” in another window. Do you have any clue on this ? Sincerly

– Theo 2022-03-21 14:50 UTC

---

Sadly, I have never used this again, so I do t know.

– Alex 2022-03-21 15:07 UTC