Skip to content Skip to sidebar Skip to footer

How To Convert Image Areas To White Or Transparent?

I'm trying convert to white or transparent some rectangles areas within the below image. I'm able to make it with ImageMagick with the following command, that first makes transpar

Solution 1:

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.

Solution 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

Post a Comment for "How To Convert Image Areas To White Or Transparent?"