ImageMagick: Example didn't work

Hi all,

I’ve been trying to recreate one of the examples from ImageMagick > Usage, but to no avail. :frowning: I copied the code, but the resulting image still had a background. How can I solve this problem?


I downloded this picture

and this picture

from the website and stored it in a separate folder. Then I opened a terminal navigated to this folder and used this command:
magick overlay_figure.gif overlay_bgnd.gif -compose ChangeMask -composite overlay_removed.png

to get this image

Archlinux with ImageMagick 7.1.1-32.

I tried this as well, but the background wasn’t gone. :frowning:

My code:

magick Frau.png hintergrund.png \ -compose ChangeMask -composite Ergebnis.png




Can you post hintergrund.png as well, so I could try to reproduce it.

I did as @Thomas_Do did, and it worked fine, using IM v7.1.1-20 on Windows 11. The background was “removed”, which means the background pixels were made transparent.

magick Frau.png hintergrund.png \ -compose ChangeMask -composite Ergebnis.png

There is a stray backslash in that command. The IM documentation assumes the shell is bash. In bash, a backslash at the end of the line means the command is continued on the next line. In other places, it means the next character should be treated as a literal. If you use bash, it should be harmless in that command, but I suggest it is removed.

Please post Frau.png and hintergrund.png.

Thanks! :wink:





It seems to me that ‘hintergrund.png’ is being ignored for some reason. And: I removed the backslash, but it didn’t help.

The background is not identical in the two images. We can use -fuzz 20%, so colours that are within 20% of each other are considered equal.

magick Frau.png hintergrund.png -fuzz 20% -compose ChangeMask -composite r.png

Ok, now it works. Thanks. :wink:


P. S.: After taking a closer look at the result, I noticed that it has “holes” in the foreground. (the light blue blouse) :frowning:


Using a guided filter (or another type such as detail/frequency-related ones) to refine the mask may help. I do not have the time to assist, but that may be one direction that you could explore. Also, GIMP has some neat selection tools if command line is unintuitive.

Cases like this often involve fiddling around, and perhaps combining various factors. In this case, most of the blue background pixel have zero in the red channel, and some have values up to 2%. So:

magick Frau.png -alpha set -channel A -fx "(r < 0.02) ? 0 : 1" x.png