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 HelpProcedure 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!

Tags:

Comments

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


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:

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:

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


Please make sure you contribute only your own work, or work licensed under the GNU Free Documentation License. Note: in order to facilitate peer review and fight vandalism, we will store your IP number for a number of days. See Privacy Policy for more information. See Info for text formatting rules. You can edit the comment page if you need to fix typos. You can subscribe to new comments by email without leaving a comment.

To save this page you must answer this question:

Please say HELLO.