I expect this can be done entirely with a G’MIC script.
G’MIC includes programming constructs (loops, conditionals, etc) that ImageMagick (IM) does not include. Doing the job with IM also needs some work in a shell script (eg bash or Windows BAT). However, I know IM better than G’MIC, so that is how I would do it.
As usual, there are many possible methods. Overall, we need to (a) find the coin, (b) infill the coin, (c) make a 2cm scale slightly smaller than the coin and (d) composite that over the image.
(Wikipedia says a 5 euro cent coin is 21.25mm diameter.)
What visually distinguishes the coin? It is a dark circle on a white background. In the OP example, it is also a distinct colour. I won’t rely on that, because an image might have dark brown twigs the same colour as the coin.
So I assume the coin is the only dark roughly circular object. I also assume the coin never overlaps with plants.
I threshold the image so we get black objects on a white background. Then I list each black object, the “connected components”. The bounding box of the coin will be roughly square. I will assume that only one bounding box is roughly square. If there are many, we could also compare the size (in pixels) of the component with the area of the bounding box; if the component is roughly pi * r^2 then we assume it is circular.
The -morphology
denoises.
For inpainting, we expand the bounding box by 10 pixels in each dimension, so we include the shadow of the coin.
Windows BAT script:
set Infile=coinPlant.jpeg
set Outfile=coin2cm.jpeg
set NumSq=0
for /F "usebackq tokens=1-5 delims=: " %%A in (`%IMG7%magick ^
%Infile% ^
-threshold 50%% ^
-morphology open disk ^
+write x1.png ^
-define "connected-components:verbose=true" ^
-define "connected-components:area-threshold=1000" ^
-connected-components 8 ^
info:`) do (
if "%%E"=="srgb(0,0,0)" (
for /F "tokens=1-4 delims=x+" %%G in ("%%B") do (
set WW=%%G
set HH=%%H
set XX=%%I
set YY=%%J
echo GHIJ= %%G %%H %%I %%J
echo WHXY= !WW! !HH! !XX! !YY!
set /A HHlo=!HH!*90/100
set /A HHhi=!HH!*110/100
if !WW! gtr !HHlo! if !WW! lss !HHhi! (
echo Squarish
set /A NumSq+=1
set /A coinXX=!XX!
set /A coinYY=!YY!
set /A sqWW=!WW!+20
set /A sqHH=!HH!+20
set /A sqXX=!XX!-10
set /A sqYY=!YY!-10
set /A twoCm=!WW!*2000/2125
)
)
echo %%A %%B %%C %%D %%E %%F
)
)
echo NumSq=%NumSq%
if not %NumSq%==1 (
echo %0: %Infile% found %NumSq% squarish components.
exit /B 1
)
echo Found one squarish bounding box: %sqWW% x %sqHH% + %sqXX% + %sqYY%
echo twoCm = %twoCm%
rem Make the coin transparent.
%IMG7%magick ^
%Infile% ^
( +clone ^
-fill White -colorize 100 ^
-fill Black -draw "translate +%sqXX%+%sqYY% rectangle 0,0,%sqWW%,%sqHH%" ^
+write x2.png ^
) ^
-alpha off ^
-compose CopyOpacity -composite ^
coinTrans.png
rem Fill the hole
%IM7DEV%magick ^
coinTrans.png ^
-process 'fillholes wr 3 lsr 50 cp onepixel auto_repeat verbose' ^
-process 'fillholes wr 3 lsr 50 cp onepixel auto_repeat verbose' ^
+write x3.png ^
( -size %twoCm%x30 xc:None ^
-fill Black ^
-draw "rectangle 0,2,%%[fx:w-1],4" ^
-draw "rectangle 0,0,0,6" ^
-draw "rectangle %%[fx:w-1],0,%%[fx:w-1],6" ^
-draw "rectangle %%[fx:w/2],0,%%[fx:w/2],6" ^
-pointsize 20 ^
-gravity Center -annotate +0+0 "2 cm" ^
+write x4.png ^
) ^
+gravity ^
-geometry +%coinXX%+%coinYY% ^
-compose Over -composite ^
%Outfile%
Each line like “+write xN.png
” is only for debugging, and can be removed.
This uses my Process module fillholes, which needs IM to be rebuilt. If you don’t want to rebuild IM, my page Filling holes shows alternative methods.
The result, coin2cm.png
, is: