I’m completely new to image processing and Gegl, and don’t know a lot about the inner workings of things, yet.
My approach to applying the mask is to convert its black to alpha (gegl:color-to-alpha
operation), and then apply this information to the actual layer (gegl:opacity
operation). But the gegl:opacity
operation seems to somehow round the alpha values? So instead of a nice, soft transition, the result is a sharp edge, as illustrated below.
Gimp project structure | desired result |
---|---|
actual output | after gegl:color-to-alpha | after gegl:opacity | |
---|---|---|---|
black to alpha | |||
white to alpha (easier to see what’s happening) |
My Gegl graph
o = output i = input a = aux
| buffer-source |
| o -----------------------------------+
| fg_node | |
|
|
i
| buffer-source | | color-to-alpha | | opacity |
| o ---- i o ---- a |
| mask_node | | cta_node | | blend_node |
o
|
|
| buffer-source | | over | |
| o -------------- i a ---+
| bg_node | | compose_node |
o
|
| png-save | |
| i --------+
| sink |
My code so far
# get image
img = Gimp.list_images()[0]
# get Gimp objects
l_foreground = img.get_layer_by_name("foreground")
l_background = img.get_layer_by_name("background")
mask = l_foreground.get_mask()
# Gegl graph root
graph = Gegl.Node()
# foreground
fg_node = graph.create_child("gegl:buffer-source")
fg_node.set_property("buffer", l_foreground.get_buffer())
# mask
mask_buffer = mask.get_buffer()
mask_node = graph.create_child("gegl:buffer-source")
mask_node.set_property("buffer", mask_buffer)
# background
bg_node = graph.create_child("gegl:buffer-source")
bg_node.set_property("buffer", l_background.get_buffer())
# convert color black to alpha
cta_node = graph.create_child("gegl:color-to-alpha")
# apply opacity of aux to input?
blend_node = graph.create_child("gegl:opacity")
# lay (processed) foreground over background
compose_node = graph.create_child("gegl:over")
# export .png file
sink_node = graph.create_child("gegl:png-save")
sink_node.set_property("bitdepth", 8)
sink_node.set_property("path", "/path/to/result.png")
# connect the nodes
compose_node.link(sink_node)
blend_node.connect("output", compose_node, "aux")
cta_node.connect("output", blend_node, "aux")
mask_node.link(cta_node)
fg_node.link(blend_node)
bg_node.link(compose_node)
# process
sink_node.process()
I also tried to use the gegl:extract-component
operation with property component = "Alpha"
, but it produces the same result as gegl:opacity
.
Am I even taking the right approach, or is the opacity operation not meant to do what I hope it does?
Tanks for any help in advance.