merge/average multiple layers weighted by alpha
Posted: 2018-01-20T04:44:10-07:00
Hello everybody, hopefully I did not overlook something when browsing the forum.
I aim to merge multiple layers weighted by alpha value of each pixel.
What doe’s I mean?
Let’s say I have 3 layers (same size) with semi transparency (alpha gradient), then the opacity should be the factor for the color values.
Example 1:
one pixel has the following RBG and alpha values:
Layer 1: R 158, G 24, B 210, alpha 0.4
Layer 2: R 240, G 0, B 128, alpha 0.1
Layer 3: R 17, G 194, B 96, alpha 0.3
Calculation for red-channel:
158 * 0.4 = 63.2
240 * 0.1 = 24
17 * 0.3 = 5.1
Sum is 92.3, this must be divided by the sum of the alpha values (0.8 ) = 115.375
Same way for green and blue channel, then the output should be: R 115, G 85, B 157, alpha 0.8
Example 2:
another pixel has the following RBG and alpha values:
Layer 1: R 18, G 241, B 185, alpha 0.7
Layer 2: R 192, G 13, B 21, alpha 1.0
Layer 3: R 92, G 120, B 210, alpha 0.4
Calculation for red-channel:
18 * 0.7 = 12.6
192 * 1.0 = 192
92 * 0.4 = 36.8
Sum is 241.4, divided by the sum of the alpha values (2.1) = 114.95…
(Here the sum of alpha values is greater ten 1, logically the result is set to 1.)
Output should be: R 115, G 109, B 112, alpha 1.0
Currently I do read all layers with pixel enumeration and do all that calculation for every pixel of all layers (using PowerShell), write the results into a TXT-File and finally convert this into PNG.
This works very accurate, the result looks how I expect it, but this way is extremely CPU intensive and not applicable for some hundreds or thousands of high resolution layers.
I already tried several -compose, - average, -mean commands, but the result is never what I’m looking for.
Hopefully someone understands what I mean and may have an idea for a more efficient way than calculating every single pixel outside ImageMagick.
Thanks a lot for suggestions!
(Windows 10, ImageMagick 7.0.3 (stand alone))
I aim to merge multiple layers weighted by alpha value of each pixel.
What doe’s I mean?
Let’s say I have 3 layers (same size) with semi transparency (alpha gradient), then the opacity should be the factor for the color values.
Example 1:
one pixel has the following RBG and alpha values:
Layer 1: R 158, G 24, B 210, alpha 0.4
Layer 2: R 240, G 0, B 128, alpha 0.1
Layer 3: R 17, G 194, B 96, alpha 0.3
Calculation for red-channel:
158 * 0.4 = 63.2
240 * 0.1 = 24
17 * 0.3 = 5.1
Sum is 92.3, this must be divided by the sum of the alpha values (0.8 ) = 115.375
Same way for green and blue channel, then the output should be: R 115, G 85, B 157, alpha 0.8
Example 2:
another pixel has the following RBG and alpha values:
Layer 1: R 18, G 241, B 185, alpha 0.7
Layer 2: R 192, G 13, B 21, alpha 1.0
Layer 3: R 92, G 120, B 210, alpha 0.4
Calculation for red-channel:
18 * 0.7 = 12.6
192 * 1.0 = 192
92 * 0.4 = 36.8
Sum is 241.4, divided by the sum of the alpha values (2.1) = 114.95…
(Here the sum of alpha values is greater ten 1, logically the result is set to 1.)
Output should be: R 115, G 109, B 112, alpha 1.0
Currently I do read all layers with pixel enumeration and do all that calculation for every pixel of all layers (using PowerShell), write the results into a TXT-File and finally convert this into PNG.
This works very accurate, the result looks how I expect it, but this way is extremely CPU intensive and not applicable for some hundreds or thousands of high resolution layers.
I already tried several -compose, - average, -mean commands, but the result is never what I’m looking for.
Hopefully someone understands what I mean and may have an idea for a more efficient way than calculating every single pixel outside ImageMagick.
Thanks a lot for suggestions!
(Windows 10, ImageMagick 7.0.3 (stand alone))