I have a big black canvas. I then read a sequence of images, same size as the canvas. They're mostly black, but each has a single, white splotch. I composite all these splotches in the single big image. The splotches don't overlap. I want to place a label in the center of each white splotch, but I don't know how to determine the coordinates of the center of a splotch. I could use a Trim on each individual splotch image, to get the bounding box of the splotch and find its center. But this doesn't give me the offset to properly place the label in the big image. How can I do this? I use an old version of IM (6.8.9) with Perl (5.16), but I don't think it matters.
If your splotches arrive, individually and unlabelled, on a canvas the same size as the image you have shared, they must presumably look like this:
That means you can get the width, height, x-offset and y-offset like this:
read w h x y <(magick Luyti.png -format %@ info: | tr 'x+' ' ')
which gives these 4 values:
75 93 23 42
And then you can calculate the x and y centres with:
((cx=x+(w/2)))
((cy=y+(h/2)))
which yields 60 and 88 and I have marked with a red circle:
You can now generate a label and measure its size like this:
magick -pointsize 14 -background none -fill blue label:"Label 1" label.png
read lw lh < <(magick label.png -format "%w %h" info:)
which yields 48 and 17.
Now get the offset to the top left of the label, by subtracting half its width and height from the centre of the splotch:
((left=cx-(lw/2)))
((top=cy-(lh/2)))
which yields 36 and 80.
Then splat the label on at that offset:
magick Luyti.png label.png -geometry +${left}+${top} -composite result.png