Gimp-Forum.net
Issues trying to Compose an image with masked channels - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions)
+---- Thread: Issues trying to Compose an image with masked channels (/Thread-Issues-trying-to-Compose-an-image-with-masked-channels)



Issues trying to Compose an image with masked channels - pronay - 05-10-2023

Hello,

This is my first time trying to write a Python script in GIMP so I am still learning my way around this stuff.
I am trying to implement a script by which I can perform the following steps:

1. Decompose an Image into RGBA channels (Colors->Components->Decompose->Color Model: RGBA)
2. Recompose an Image using R,G channels with B and A using Mask Value of 255 (Colors->Components->Compose->Color Model: RGBA, B and A use 255 mask value)

To do this in Python, I am attempting the following steps:
Code:
       decomposed = pdb.plug_in_decompose(img, drawable, "RGBA", 0)
   
       gimp.message("Performed Decompose")

       # Grab the name of the original image
       fileNameWithExt = pdb.gimp_item_get_name(draw)
       fileNameNoExt = fileNameWithExt.split('.')

       # Create the layers
       gimp.message("Creating new Layers")
       gimp.message("Setting up Red Layer")
       layerR = pdb.gimp_layer_new_from_drawable(pdb.gimp_image_get_active_layer(decomposed[0]), decomposed[0])
       layerName = fileNameNoExt[0] + "_RGBA_Red"
       pdb.gimp_item_set_name(layerR, layerName)
       pdb.gimp_image_insert_layer(decomposed[0], layerR, None, 0)

       gimp.message("Setting up Green Layer")
       layerG = pdb.gimp_layer_new_from_drawable(pdb.gimp_image_get_active_layer(decomposed[1]), decomposed[1])
       layerName = fileNameNoExt[0] + "_RGBA_Green"
       pdb.gimp_item_set_name(layerG, layerName)
       pdb.gimp_image_insert_layer(decomposed[1], layerG, None, 1)

       gimp.message("Setting up Blue Layer")
       layerB = pdb.gimp_layer_new_from_drawable(pdb.gimp_image_get_active_layer(decomposed[2]), decomposed[2])
       layerName = fileNameNoExt[0] + "_RGBA_Blue"
       pdb.gimp_item_set_name(layerB, layerName)
       pdb.gimp_image_insert_layer(decomposed[2], layerB, None, 2)

       gimp.message("Setting up Alpha Layer")
       layerA = pdb.gimp_layer_new_from_drawable(pdb.gimp_image_get_active_layer(decomposed[3]), decomposed[3])
       layerName = fileNameNoExt[0] + "_RGBA_Alpha"
       pdb.gimp_item_set_name(layerA, layerName)
       pdb.gimp_image_insert_layer(decomposed[3], layerA, None, 3)
   
       gimp.message("Creating Layer Mask")
       channelB = pdb.gimp_layer_create_mask(layerB, 1)
       channelA = pdb.gimp_layer_create_mask(layerA, 1)

       gimp.message("Inserting Channels to Decomposed Images")
       pdb.gimp_image_insert_channel(decomposed[2], channelB, None, 0)
       pdb.gimp_image_insert_channel(decomposed[3], channelA, None, 0)

       # Now compose the new image into the target export image
       gimp.message("Starting Compose Step for Output")
       OutImage = pdb.plug_in_compose(decomposed[0], draw, decomposed[1], decomposed[2], decomposed[3], "RGBA")
       gimp.message("Done with Composing Output")

However, when I run this script, I see that my output image is the exact same as the input. This means that the 255 masks are not being applied to the output composed image.
Is the approach I am using correct here? How should I be applying the 255 Mask Value to the B and A channels similar to the settings available for the Compose option in the UI?

Any advice here would be much appreciated.


RE: Issues trying to Compose an image with masked channels - Ofnuts - 05-11-2023

it looks like you want to add layer masks, but you are not adding them to the layers (gimp-layer-add-mask) and are instead adding them as channels to the image.

Now, IMHO compose/decompose is a thing of a distant past. In modern Gimp you can get essentially the same result by using layers filled with #FF0000, #00FF00, and #0000FF, in multiply mode over copies of the image (merged down or each kept in a group). In the example below, each group is an extracted color channel (the other two are set to 0) as can be seen in the group thumbnail. When the top two groups are set to addition mode, this adds the three channels together and you recover the initial image.

[attachment=9786]



RE: Issues trying to Compose an image with masked channels - pronay - 05-11-2023

(05-11-2023, 12:12 AM)Ofnuts Wrote: it looks like you want to add layer masks, but you are not adding them to the layers (gimp-layer-add-mask) and are instead adding them as channels to the image.

Now, IMHO compose/decompose is a thing of a distant past. In modern Gimp you can get essentially the same result by using layers filled with #FF0000, #00FF00, and #0000FF, in multiply mode over copies of the image (merged down or each kept in a group). In the example below, each group is an extracted color channel (the other two are set to 0) as can be seen in the group thumbnail. When the top two groups are set to addition mode, this adds the three channels together and you recover the initial image.

This is great! Thanks for pointing this out, If I wish to extract information of the Alpha channel here is it possible for me to do so? I don't suppose I can use #000000FF (atleast this doesn't seem to make sense intuitively to me). 

What would be the way to go about having information of the Alpha channel here?

Thank you for your response


RE: Issues trying to Compose an image with masked channels - Ofnuts - 05-11-2023

What kind of information about the alpha channel? If you do an alpha-to-selection the selection is a copy of the alpha channel of the layer, and you can save it to a channel. You can also create a layer mask and initialize it to the alpha-channel of the layer. In both case you obtain a drawable that is a copy of the layer's alpha channel. The first technique is a bot more suitable if you don't want to modify it, the second is better if you intend to apply back changes.

But I have the nagging feeling that this is a XY problem. What are you trying to achieve?


RE: Issues trying to Compose an image with masked channels - pronay - 05-11-2023

I'm trying to avoid this being an XY problem, what I want to achieve remains the same as my original post.

I am trying to write a script that can essentially do what the decompose to R,G,B,A and then compose a new image using R and G as-is but with B and A channels using a mask value of 255.

If the new approach being suggested (using layers filled with #FF0000, ##00FF00 and ##0000FF in multiply mode) is the better approach, I can try this on my end. I'll use alpha-to-selection and then apply a 255 mask value to the drawable. 

I'll be giving this a go and updating my results here



RE: Issues trying to Compose an image with masked channels - Ofnuts - 05-11-2023

(05-11-2023, 05:17 PM)pronay Wrote: I'm trying to avoid this being an XY problem, what I want to achieve remains the same as my original post.

I am trying to write a script that can essentially do what the decompose to R,G,B,A and then compose a new image using R and G as-is but with B and A channels using a mask value of 255.

If the new approach being suggested (using layers filled with #FF0000, ##00FF00 and ##0000FF in multiply mode) is the better approach, I can try this on my end. I'll use alpha-to-selection and then apply a 255 mask value to the drawable. 

I'll be giving this a go and updating my results here

If you want to "fill" the alpha channel with 255, you have to wonder what the colors of the transparent-now-opaque pixels is. In Gimp th is is normally the current "background" color, and filling the channel with 255 is the same as removing it.

And filling the image with B=255 is the same just adding a layer filled with pure blue (#0000FF) and put in in "Addition" mode, so you don't even need the decomposition.