0

I have a large number of images I'd like to remove the background color of and replace with 100% transparent background. I also have semi-transparent rectangles which I need to "color correct".

To illustrate my case here is an example PNG image: Magenta background. 40% transparency yellow rectangle on top of a fully opaque blue circle.

enter image description here

Removing the background, which is straight forward, I get this:

enter image description here

So now my question is how do I get the rectangle to be a transparent yellow instead of the orange tint it originally had, while at the same time keep the color of the overlapping area between circle and rectangle?

enter image description here

I'm looking for any kind of solution. It could be program specific or code based (algorithm).

15
  • 1
    Indexed colour doesn't support semi-transparency.
    – Billy Kerr
    Jan 14 at 10:50
  • @BillyKerr my bad. I am also converting these images to rgba
    – Jonas
    Jan 14 at 10:52
  • What information there is (either in the image file or inputted to the automate, not only between your ears) of the original color content? What data (=cold undeniable facts, no guesses) the wanted automate would use to decide there was no orange, but 40% transparent yellow on magenta? One such fact could be "the artist did his whole life work by drawing only circles and rectangles, there's no other forms".
    – user287001
    Jan 14 at 12:05
  • @user287001 well the bg color is magenta (#FF00FF) and nothing else. Magenta is specifically used for the bg and never used on any shape or object. The rectangle's transparency level and base color are known (40%, #FFFF00). Finally, the image's colors are limited to a palette, so no anti-alias or blurred-in pixels
    – Jonas
    Jan 14 at 12:51
  • 1
    Does the example image you post here resemble the actual image you want to manipulate? Or is it very simplified? The paradox seems to be that for such a simple image it's quick to draw it again manually, but for a large and complex image it's impossible to automate because no program or script will be able to know whether an orange pixel is "truly" orange or comes from overlaying a transparent yellow.
    – Wolff
    Jan 15 at 16:24
3

Here is a recipe for an action in Photoshop, which works for this exact image (or other images using the same colors and principle). I suspect that the images you want to process have might have other or additional colors or some unknown complexity which could make this method worthless (or at least in need of tweaking).

You mention in comments that your images use a fixed palette and don't have any anti-alias or blurred-in pixels. But the example you post actually has anti-aliasing, so I start out by reducing the number of used colors to exactly 4 so the example image matches what you say you have:


  • In the Actions panel, press Create new action and Record to start recording a new action.

  • Double-click the Background layer to convert it to an ordinary layer. (If it already is, you can omit this.)

  • Press Ctrl / Cmd + A to select all.

  • Press Ctrl / Cmd + C to copy selection to clipboard.

  • Press Ctrl / Cmd + D to deselect.

  • Use Image > Adjustments > Channel Mixer with the following settings to create a grayscale image where the magenta and orange pixels become white.

  • Use Image > Adjustments > Threshold with the following settings to make every non-white pixel black.

  • Use Image > Adjustments > Gradient Map setup to go from blue (RGB(0, 0, 255)) to white to turn all black pixels blue and leave the white pixels unchanged.

  • Double-click the layer to enter its Blending Options. Under Blend If > This Layer, pull the rightmost slider one tick to the left to make the white pixels transparent.

  • Press Ctrl / Cmd + V to paste the copy of the original we made before into a new layer.

  • Use Image > Adjustments > Channel Mixer with the following settings to create a grayscale image where the magenta and blue pixels become white.

  • Use Image > Adjustments > Threshold with the following settings to make every non-white pixel black.

  • Use Image > Adjustments > Gradient Map setup to go from yellow (RGB(255, 255, 0)) to white to turn all black pixels yellow and leave the white pixels unchanged.

  • Double-click the top layer to enter its Blending Options. Under Blend If > This Layer, pull the rightmost slider one tick to the left to make the white pixels transparent.

  • In the Layers panel, set the Opacity of the top layer to 60% (I just measured this).

  • Use Layer > Merge Visible to merge the two layers into one.

This should give you the wanted result for an image with these specific values.

2

I guess no automate can decide how to construct the wanted original if there's no other facts than used colors - the same result can be got with numerous different layerings.

I guess redrawing with layers is not especially pleasant option if the actual image is complex and has a wide palette. But you can try to make selections with the magic wand. It should work well if there's no edge blurs. Turn anti-alias OFF. You can insert and subtract parts to or from the selection by holding Shift or Alt keys.

Try also Select > Color range with zero fuzziness.

Here the bottom layer is used to make the selections and color fills (blue and yellow) are inserted to different new layers :

enter image description here

Magic wand tool options allow selections in certain selected layer, only remember to switch to a new empty layer before inserting color.

I do not recommend using directly without selection Paint Bucket's possibility to fill certain color tolerance area. Making a selection gives a possibility to see the area to be colored.

Finally the transparency is inserted to the known transparent color layers and the original is closed:

enter image description here

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.