0

I'm trying convert to white or transparent some rectangles areas within the below image. enter image description here

I'm able to make it with ImageMagick with the following command, that first makes transparent desired colors and finally convert to black the rest with "negate".

convert input.png \
-transparent '#4B8DF8'  \
-transparent '#27A9E3'  \
-transparent '#2295C9'  \
-transparent '#E7191B'  \
-transparent '#C91112'  \
-transparent '#28B779'  \
-transparent '#17A769'  \
-transparent '#852B99'  \
-transparent '#751E88'  \
-transparent '#D84A38'  \
-transparent '#B4CEF8'  \
-transparent '#17A76A'  \
-transparent '#CA1112'  \
-transparent '#2296CA' \
-transparent '#DDE8FA' \
-alpha extract -negate out_convert.png

The Python/Wand script I have so far is this:

from wand.image import Image

with Image(filename='input.png') as img:
    img.transparent_color('#4B8DF8', alpha=0.0)
    img.transparent_color('#27A9E3', alpha=0.0)
    img.transparent_color('#2295C9', alpha=0.0)
    img.transparent_color('#E7191B', alpha=0.0)
    img.transparent_color('#C91112', alpha=0.0)
    img.transparent_color('#28B779', alpha=0.0)
    img.transparent_color('#17A769', alpha=0.0)
    img.transparent_color('#852B99', alpha=0.0)
    img.transparent_color('#751E88', alpha=0.0)
    img.transparent_color('#D84A38', alpha=0.0)
    img.transparent_color('#B4CEF8', alpha=0.0)
    img.transparent_color('#17A76A', alpha=0.0)
    img.transparent_color('#CA1112', alpha=0.0)
    img.transparent_color('#2296CA', alpha=0.0)
    img.transparent_color('#DDE8FA', alpha=0.0)
    img.negate()
    img.save(filename='out_python.png')

And below I show the output I get using convert command (only black and white) and the output with python/wand script (it still has some other colors beside black and white).

enter image description here

What is missing in my script in order to get the same output as I get with ImageMagick? Is Wand a good python library for this or could be done with another one?

martineau
  • 119,623
  • 25
  • 170
  • 301
Ger Cas
  • 2,188
  • 2
  • 18
  • 45

2 Answers2

3

You can convert to white all colors but black simply by thresholding your image in ImageMagick.

convert rectangles.png -threshold 0 rectangles2.png

Or just make all colors but white into white

convert rectangles.png -fill white +opaque white rectangles3.png

Or just colorize the image to white

convert rectangles.png -fill white -colorize 100 rectangles4.png

Do I misunderstand your problem?

If you have identified all colors, but they are not getting converted to white in your code, it is because some of the colors are very slightly different. So just add -fuzz XX% before your first -transparent command. Try XX=0% to start and increase as needed.

ADDITION:

I suspect this is what you want. You were close. You just needed to add some fuzz value. But rather than making it transparent, I convert directly to white using opaque_fill().

Input:

enter image description here

from wand.image import Image
from wand.display import display

with Image(filename='color_rectangles.png') as img:
    img.opaque_paint(target='#5f8bfc', fill='white', fuzz=0.30*img.quantum_range, invert=False)
    img.opaque_paint(target='#43ad49', fill='white', fuzz=0.30*img.quantum_range, invert=False)
    img.opaque_paint(target='#831d98', fill='white', fuzz=0.30*img.quantum_range, invert=False)
    img.save(filename='color_rectangles_fill_white.png')
    display(img)

enter image description here

Note that one could use as little as 0.05 factor of quantum_range and get most of the color changed, but due to antialiasing in drawing your boxes, you need to increase it as much as possible to remove the outline without changing other colors.

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • Thanks for your help. Actually I try to replicate in python, what ImageMagick does. If I can get the same with ImageMagick in a shorter `convert` command, would be easier I think to replicate it in Python. I tried your 3 commands but with the 3 I get a complete white output image. I want to convert to white for example, the blue, green, red, purple (the colors I list) and the rest to black. I begin converting to transparent since in input.png there is actually white areas that I'm not interested in. – Ger Cas Sep 12 '20 at 02:07
  • See my addition to my answer – fmw42 Sep 12 '20 at 03:11
  • Thanks so much. it's very close to what I'm looking for now. I have to questions. Why you use only 3 colors and even different to those I use and you get the output almost correct? (since still a orange rectangle remains) . The other question, after convert the rectangles to white, how to make them transparents? this in order to negate to finally get all rectangles transparents and the rest in black? – Ger Cas Sep 12 '20 at 04:52
  • You said you only wanted to convert those colors. So I measured the rectangles with some other tool, which gave slightly different values. That is common especially in JPG, which changes colors due to compression. Also measuring tools that read from the display can read slightly different values. I did not know which of your colors were the ones in question. The reason it works is that the colors are off from your values. So I needed to add a big -fuzz value, which allows a tolerance to the colors that would be changes. Also the outline of the boxes is even more different due to anti-aliasing. – fmw42 Sep 12 '20 at 06:24
  • Thanks for your answer. Accepted since has helped me a lot and you share a pretty nice way to get the goal. Regards – Ger Cas Sep 12 '20 at 06:50
2

What is missing in my script in order to get the same output as I get with ImageMagick?

You're missing the equivalent to -alpha extract. Just add img.alpha_channel = 'extract', and the two outputs should match.

 with Image(filename='input.png') as img:
    img.transparent_color('#4B8DF8', alpha=0.0)
    img.transparent_color('#27A9E3', alpha=0.0)
    img.transparent_color('#2295C9', alpha=0.0)
    img.transparent_color('#E7191B', alpha=0.0)
    img.transparent_color('#C91112', alpha=0.0)
    img.transparent_color('#28B779', alpha=0.0)
    img.transparent_color('#17A769', alpha=0.0)
    img.transparent_color('#852B99', alpha=0.0)
    img.transparent_color('#751E88', alpha=0.0)
    img.transparent_color('#D84A38', alpha=0.0)
    img.transparent_color('#B4CEF8', alpha=0.0)
    img.transparent_color('#17A76A', alpha=0.0)
    img.transparent_color('#CA1112', alpha=0.0)
    img.transparent_color('#2296CA', alpha=0.0)
    img.transparent_color('#DDE8FA', alpha=0.0)
    img.alpha_channel = 'extract'
    img.negate()
    img.save(filename='out_python.png')

out_python.png

emcconville
  • 23,800
  • 4
  • 50
  • 66
  • Thanks so much for your answer. It works pretty nice. I had issues to find the correct equivalent in wand. – Ger Cas Sep 12 '20 at 23:23