问题
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 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).
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?
回答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:
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)
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.
回答2:
What is missing in my script in order to get the same output as I get with ImageMagick?
You're missing the wand 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')
来源:https://stackoverflow.com/questions/63855588/how-to-convert-image-areas-to-white-or-transparent