Compositing images with ImageMagick

I locked my camera down so it could take consistent pictures of a room with the same angle and lighting. I took a picture of the empty room, then I took images of myself in various poses in separate places in the room, now I have a bunch of them and I want to composite them all into one image of me all over the room at the same time. At the moment, I do not have to worry about anything overlapping or complex shadows, but once I have the basics down I’d like to handle that kind of thing too.

If I’m reading the ImageMagick website right, there is a way to do this, by subtracting one image from another, then adding all the differences together back to the background-only image, but nothing is coming out right, I keep getting things like white-on-black images, or greyscale weirdness. All the images are PNGs.

I want to do this from the command line. My ultimate goal is to create a batch script file to do this over and over from the cmd line with multiple image sets that each share the same background image. I’m doing this in windows 11, if it matters, but I think all versions of ImageMagick share the same command syntax.

Someone with better expertise will likely come along soon, but it sounds like the black/white and other images you have generated are intended to be used as masks for the compositing. This page might give you more of an idea of how those are to be used.

Thanks, that particular article wasn’t what I needed, but I persisted with googling and eventually figured out it’s a 2-step process needing a temporary file to hold my foreground object with a transparent background. Once I had the correct syntax, I fed it into ChatGPT-4 to get this batchfile:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set back_ground=%1
set pattern=%2
set composited_result=%3

REM Check if back_ground file exists
if not exist "%back_ground%" (
    echo Background file %back_ground% not found.
    exit /b 1
)

REM Check if the pattern matches any files
set count=0
for %%A in (%pattern%) do (
    set /a count+=1
)
if %count% lss 1 (
    echo No files match the pattern: %pattern%
    exit /b 1
)

REM Copy back_ground file to composited_result
echo Copying %back_ground% to %composited_result%
copy "%back_ground%" "%composited_result%"

REM Process matched files
set count=0
for %%A in (%pattern%) do (
    set /a count+=1
    set cmd1=magick "%%A" "%back_ground%" -compose ChangeMask -composite temp_foreground_figure.png
    echo !cmd1!
    !cmd1!
    set cmd2=magick convert -composite "%composited_result%" temp_foreground_figure.png "%composited_result%"
    echo !cmd2!
    !cmd2!
)

echo Processed %count% matches for the pattern: %pattern%

ENDLOCAL