1

I have an image of a solid-black graphic with anti-aliased edges on a shaded-red background, and an image of the background without the graphic, and I want to recover an image of the graphic without the background. My goal is a solid-black layer with a layer mask that reproduces the original graphic as an all-black image with an Alpha channel.

I can calculate the necessary darkness value K for each pixel of the layer mask with the equation K = round(100 (1 - (C_{BG} - C_{out}) / C_{BG})). However, doing this for each pixel one at a time would be incredibly tedious. Is this something that can be automated with a script?

I have a Photoshop file with the graphic-and-background as Layer 1, the background-without-graphic as Layer 2, and a solid-black layer with a layer mask as Layer 3. I want to have a script that:

  • looks at one pixel in the image,
  • reads the color of that pixel on Layer 1 and sets that as the variable C_{BG},
  • reads the color of that pixel on Layer 2 and sets that as the variable C_{out},
  • calculates the variable K = round(100 (1 - (C_{BG} - C_{out}) / C_{BG})),
  • sets the darkness value K of that pixel on the layer mask of Layer 3, and
  • moves to the next pixel in the image

until it has covered all the pixels.

I know Photoshop supports scripts, but I've never used any, so I don't know how to go about setting one up, or if it's even possible to achieve my goal here.

3
  • Theres calculations command. But you will find that by the time youve done this in photoshop you would have done it in imagemagick 25 times even if you didnt have it installed.
    – joojaa
    Jul 27 at 15:23
  • @joojaa can you explain how to do this in ImageMagick, then? I'm not familiar with that program.
    – Lawton
    Jul 27 at 15:26
  • I feel that if you had bothered to read comments you would have already known this. graphicdesign.stackexchange.com/questions/154536/… But yeah magick fg.png bg.png -fx "1 - (u[0] - u[1]) / u[1]" out.png
    – joojaa
    Jul 27 at 18:47

1 Answer 1

2

Not asked, but after replacing your complex variable names with B (=pixel from Layer1) and A (=pixel from layer2) your K will have equation K=Round(100(1-(B-A)/B).

It reduces in elementary math to K=Round(100(A/B))

B is obviously from the background and A is from your black icon on red.

Seemingly you aim to solve the problem in your previous question, but now you have done some tricky reasoning (see NOTE1) and found a formula which reduces to the equivalent of layer blending mode "Divide".

We can try it in GIMP which offers some flexibility beyond Photoshop when we try to make the transparent result you finally expect:

enter image description here

The white areas are caused by non-zero Green and Blue channel pixels in the icon layer. Format conversions probably have caused them. The result becomes perfect if we force in the icon layer the green and blue channels to zero for ex. with Color > Curves:

enter image description here

There's no need to work with transparency via layer masks. We can in GIMP turn red to transparency. Merge layers and make transparency possible by applying Layer > Transparency > Add alpha channel. Then apply Layer > Transparency > Color to alpha and select the Red color:

enter image description here

This should be the same you wanted to get by scripting.

BTW we have a 400 px wide versions of your images and it seems like your original images were only 50px wide. We could resample the got image to 12,5% size. The interpolation method in GIMP should be = None. That's the same as "Nearest neighbour resampling" in Photoshop.

NOTE1: The division can also be found from the general transparency blending equation: M = PT + (1-P)B where M is a channel value in the mixing result, T is the channel value of the top layer, B is the channel value in the opaque background layer and P is the opacity of the top layer.

In this case the top layer has only black, so T=0. We have M=(1-P)B.

That gives the transparency 1-P = M/B or 100(M/B) when scaled to 0...100%.

I'm afraid this is still not the solution for your original problem (=recreate the pixelixed icon and its anti-aliasing), because your icon on red and background only images are not directly comparable. The icon on red image is a layer blending result and affected by the non-linear gamma which makes the reasoned equations inaccurate. The layer blending equation should be converted to gamma-corrected one for a better result. I skip it and suggest you to redraw the image as long as you have some energy left.

1
  • Using the "Divide" blending mode with the background-only layer on top produces exactly the result I want, after correcting the dozen pixels with non-zero green and blue values. Converting the red channel of the divided-image into the Alpha mask of a solid-black layer above the background-only layer exactly reproduces the background-and-graphic image. I thought I had tried the "Divide" blending mode, but I must have only done it with the background-only layer beneath the background-and-graphic layer instead of the other way around.
    – Lawton
    Jul 28 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.