I’m implementing a divide blend mode by hand to do background removal from a bunch of images. In Photoshop, I can also set the opacity of the background layer to make it a little less harsh, like this:
But I can’t figure out how to implement that here! If I reduce the RGB pixel values in the background image it just makes the result darker.
Purely out of personal interest, could you please share the leaf image without the divide blending mode activated? I’ve applied similar settings (divide blend mode + 70% opacity) on top of various images, but can’t detect any visual difference. Or am I misunderstanding something?
PImage img_bkg, img_act, layer_blended;
float opacity = 0.5; // insert value between 0 and 1
void setup() {
size(1280, 720);
noLoop();
img_bkg = loadImage("orange.jpg");
img_act = loadImage("grapes.jpg");
layer_blended = createImage(width, height, RGB);
}
void draw() {
loadPixels();
for (int i = 0; i < img_bkg.pixels.length; i++) {
// get RGB values for all pixels of the background image
float bkgR = (img_bkg.pixels[i] >> 16 & 255);
float bkgG = (img_bkg.pixels[i] >> 8 & 255);
float bkgB = (img_bkg.pixels[i] & 255);
// get RGB values for all pixels of the image on top
float actR = (img_act.pixels[i] >> 16 & 255);
float actG = (img_act.pixels[i] >> 8 & 255);
float actB = (img_act.pixels[i] & 255);
// get RGB values for all pixels if the blending mode 'divide' would be active ...
float bldR = (bkgR / actR) * 255;
float bldG = (bkgG / actG) * 255;
float bldB = (bkgB / actB) * 255;
// ... and use it to construct a new image called 'layer_blended'
layer_blended.pixels[i] = color(bldR, bldG, bldB);
}
updatePixels();
image(img_bkg, 0, 0);
tint(255, opacity * 255);
image(layer_blended, 0, 0);
}
The article Photoshop Blend Modes Explained helped a great deal to understand the mathematics behind the blend modes. What it doesn’t explain however is how blending modes work in co-operation with different opacity settings, yet the solution is actually quite simple:
draw the background image
place a transparent version of the blended image on top of it
“We did it?” No, you did it! That’s awesome and so obvious (once you realize it ). It makes total sense that the layers in PS are essentially sitting on top of a white canvas that we don’t see. Thanks for doing the detective work!
This makes me think that a P5-focused tutorial on Photoshop layers/blend modes would be super fun.
Also, it makes sense now why the original numbers are multiplied by 255 – essentially that creates the white layer they’re sitting on, which then gets reduced when you divide by another number in the 0-255 range.
Ok, taking a closer look I realize one key question: is there a way to do this without tint? In other words, direct pixel manipulation. I’d like to avoid using a PGraphics buffer if possible and there must be a way to do this just with math, right?
P5/Photoshop tutorial sound like a great idea, go for it!
The tint can be replaced by adding a fourth parameter to the layer_blended.pixels[i]. Mayhaps we can take out or reduce the use of PImages as well, but I’m handing over that case to you fellow detective Jefflock Holmpson. Good luck and let us know if you have any breakthroughs