4

I have a colorful image that has alot of existing transparent background, but also a few blocks of hot pink rgb(255,0,255) color I want to additionally become transparent.

I'm doing so like this:

convert 0001.png -fuzz 15% -fill transparent -transparent "rgb(255,0,255)" result.png

The problem is, because the original image was anti-aliased, there is still a hot pink halo, where it's not matching e.g. rgba(255,0,255, 50 ) and other partially-transparent values, unless I turn up the -fuzz way too high.

So I figured I'd solve the problem like this:

convert 0001.png -fuzz 15% -fill transparent
-transparent "rgba(255,0,255,0)"
-transparent "rgba(255,0,255,64)"
-transparent "rgba(255,0,255,128)"
-transparent "rgba(255,0,255,192)"
-transparent "rgba(255,0,255,255)"
result.png

...but the additional calls to -transparent with varying levels of opacity don't seem to be having any additional effect.

It is almost as if calls to -transparent truncate my rgba(255,0,255,128) to rgb(255,0,255) before doing match (although -fuzz still looks at opacity, it seems to always do so starting from 255, not from the opacity I pass into -transparent).

How can I replace all examples of the color RGB(255,0,255), regardless of the pixel's pre-existing alpha?

This is the image I'm using:

enter image description here

This is what I'm being left with and I want to remove: (magnified and given white background for ease of seeing) enter image description here

2
  • 1
    Yes, I want the dashed pink removed. The call to -transparent is only getting rid of the fully opaque pink, not the semi-opaque pink, when the opacity falls below the '-fuzz' threshold, but turning up the fuzz makes -transparent also remove colors that aren't pink.
    – Jamin Grey
    Commented Nov 28, 2023 at 0:05
  • look at background removal imagemagick.org/Usage/masking/#bg_remove. - Or a -floodfill with appropriate fuzz might do the job.
    – 1NN
    Commented Nov 28, 2023 at 9:21

1 Answer 1

5
+25

...but the additional calls to -transparent with varying levels of opacity don't seem to be having any additional effect.

In rgba(R,G,B,A) the A is from 0 (fully transparent) to 1 (fully opaque), not from 0 to 255. Your attempt can be fixed like this:

convert 0001.png -fuzz 15% -fill transparent \
-transparent "rgba(255,0,255,0)" \
-transparent "rgba(255,0,255,0.25)" \
-transparent "rgba(255,0,255,0.5)" \
-transparent "rgba(255,0,255,0.75)" \
-transparent "rgba(255,0,255,1)" \
result.png

This is the result:

result, fuzz 15%

I don't know if it's the best solution. At least it shows your idea with multiple -transparent wasn't bad, you just got the range of opacity wrong.

The above result is not perfect, there are some pinkish remnants where pink has been blended with other (opaque) colors. With -fuzz 35% it looks better in this aspect:

result, fuzz 35%

But the edges lose their softness.

1
  • 1
    This was exactly what I was looking for, thank you! I get tripped up by the 255 vs 1.0 thing quite alot in other domains; I should've realized that was the problem. =/
    – Jamin Grey
    Commented Dec 7, 2023 at 15:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .