0

This is my first foray into gimp scripting, and I seem to be having some basic misunderstanding. I've written the following code which is intended to convert white into transparency in the specified file, and to export the result as a png file:

(define (edit-name original-name)
  (let* ((pos (- (string-length original-name) 4))
    (suffix (substring original-name pos))
    (prefix (substring original-name 0 pos))
    (new-name (string-append prefix ".png")))
    new-name))

(define (my-process filename)
    (let* ((color '(0 0 0))
           (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
           (drawable (car (gimp-image-get-active-layer image)))
           (filename-modified (edit-name filename)))
        (if (not (gimp-drawable-is-rgb drawable))
            (gimp-image-convert-rgb image))
        (plug-in-colortoalpha RUN-NONINTERACTIVE image drawable color)
        (file-png-save2 RUN-NONINTERACTIVE image drawable filename-modified filename-modified 0 9 1 1 1 1 1 0 1)))

This script is saved in the file ~/.gimp-2.8/scripts/my-process.scm and is invoked with:

gimp -i -b '(my-process "somefile.xcf")' -b '(gimp-quit 0)'

The script appears to run and I get output on the console saying batch command executed successfully. Furthermore, if I put an obvious syntax or parameter error into the script then I get an error. I've also tried re-getting the drawable after the rgb conversion, in case the conversion results in a different object. I've also tried using file-png-save-defaults instead of file-png-save2, to no avail.

However, the output I get is a simple png export in indexed color mode (the original mode of the source image), which does not contain the alpha channel.

I've looked at the documentation for the conversion steps I'm using and cannot figure out what's going on; help would be much appreciated.

Jeremy
  • 1,049
  • 1
  • 15
  • 28

1 Answers1

2

This works for me (this is your code in Python, using the same calls):

image=gimp.image_list()[0]
layer=image.active_layer
print "Type before:",pdb.gimp_drawable_type(layer) 
pdb.gimp_image_convert_rgb(image)
print "Type after:",pdb.gimp_drawable_type(layer) 
print "Has alpha after:",pdb.gimp_drawable_has_alpha(layer)
pdb.plug_in_colortoalpha(image,layer,(0,0,0))
pdb.file_png_save2(image,layer,'/tmp/foo.png','/tmp/foo.png',0,9,1,1,1,1,1,0,1)

And the output is (you can paste the above directly in the Python console):

>> image=gimp.image_list()[0]
>>> layer=image.active_layer
>>> print "Type before:",pdb.gimp_drawable_type(layer) 
Type before: 4
>>> pdb.gimp_image_convert_rgb(image)
>>> print "Type after:",pdb.gimp_drawable_type(layer) 
Type after: 0
>>> print "Has alpha after:",pdb.gimp_drawable_has_alpha(layer)
Has alpha after: 0
>>> pdb.plug_in_colortoalpha(image,layer,(0,0,0))
>>> pdb.file_png_save2(image,layer,'/tmp/foo.png','/tmp/foo.png',0,9,1,1,1,1,1,0,1)

What I find fishy is that there is no alpha channel after the conversion (type is RGB_IMAGE and not RGBA_IMAGE) (which is expected), but color-to-alpha works without adding one. You can however try to add one explicitly and see if this changes something.

This said, 95% of all batch processing in Gimp is better done with ImageMagick. For color-to-alpha, see this SO question

Community
  • 1
  • 1
xenoid
  • 8,396
  • 3
  • 23
  • 49
  • I can confirm that using Python results in the correct output for me, so this is starting to look like a bug on the Scheme side of things. – Jeremy Jul 17 '16 at 12:15
  • Oh, and I forgot to add: explicitly adding an alpha channel does not change the result. – Jeremy Jul 17 '16 at 15:19