How can I overlap several images on top of each other using imagemagick?
I have a base image layers.png
:
I want to cut out different parts of the image and overlap them. E.g., overlap the skeletal rib cage and blood vessels of the left arm on top of the muscle body (desired output):
So I have extracted the different parts I need:
% convert layers.png -crop 400x1048+0+0 body.png
% convert layers.png -crop 140x180+522+230 -alpha set -channel Alpha -evaluate multiply 0.8 +channel -background none -gravity north -splice x230 -gravity west -splice 122x +gravity ribs.png
% convert layers.png -crop 100x340+1434+245 -alpha set -channel Alpha -evaluate multiply 0.8 +channel -background none -gravity north -splice x245 -gravity west -splice 271x +gravity arm.png
Using -splice
to offset the body parts according to how they will be offsetted from the top-left corner in the composited image.
Compositing these images works for two at a time:
% convert body.png ribs.png -composite body_and_ribs.png
But it does not work for three at a time:
% convert body.png ribs.png arm.png -composite all.png
And my other attempt with -flatten
instead of -composite
is even further from what I want:
% convert body.png ribs.png arm.png -flatten all.png
How can I simply overlap more than two input images on top of each other in imagemagick?
To get the flattened pieces to align properly, the ImageMagick solution is to "+repage" them so they all start with the same geometry before the "-flatten".
I've put your entire operation into a single command, reading the input once and using it to make all three parts, then flatten and write the output.
magick convert layers.png -background none -write mpr:img0 +delete ^
( mpr:img0 -crop 400x1048+0+0 ) ^
( mpr:img0 -crop 140x180+522+230 ^
-channel A -evaluate multiply 0.8 +channel -splice 122x230 ) ^
( mpr:img0 -crop 100x340+1434+245 ^
-channel A -evaluate multiply 0.8 +channel -splice 271x245 ) ^
+repage -flatten body_and_ribs.png
That command is in Windows CLI syntax, and produces the image below.
The command uses IM's built-in memory register "mpr:anyname" to hold the input image for re-use several times. Inside each set of parentheses the input image gets cropped as necessary, the alpha adjusted, and spliced to locate it in the result. After the parentheses all three images get a "+repage" so they all line up properly with the "-flatten".