Search code examples
image-processingimagemagickimagemagick-convert

Merge 3 images in a single convert command mantaining size ImageMagick


With these 3 separates codes I create the following 3 images with desired size for each one. I'm failing in merging in a single command.

This code produces P1.png

convert \( \( -size 426x37 xc:"#4FA7FF" -fill black -font Calibri-Bold -pointsize 32 -gravity center -annotate +0+0 "Number 1" \) \
           \( -size 426x37 xc:"#4FA7FF" -fill black -font Calibri-Bold -pointsize 32 -gravity center -annotate +0+0 "Number 2" \) \
 +smush +2 -write P1.png \) null:
 

P1.png (854x37)

enter link description

This code produces P2.png

convert \( \( -size 881x488 xc:"#FFE97F"  \) \
           \( -size 881x488 xc:"#00FF90" \) \
            +smush +6 -resize 1180x441! -write P2.png \) null:

P2.png (1180x441)

enter link description

This code produces P3.png

convert \( \( -size 1104x89! xc:"#00137F" -fill white -font Calibri-Bold -pointsize 48 -gravity center -annotate +0+0 "Different boxes" \) \
-write P3.png  \) null:

P3.png (1104x89)

enter link description

If I join the 3 images in an image editor visually (like Paint.net) the resulting image is of 1180x606 and the resolution is 96 pixels per inch.

How can I join these 3 commands in a single "convert" command in order the final image be of 1180x606 in size?

I've tried with this code, but I don't know how to construct the command correctly

convert \ 
\( -size 426x37 xc:"#4FA7FF" -fill black -font Calibri-Bold -pointsize 32 -gravity center -annotate +0+0 "Number 1" \) \
\( -size 426x37 xc:"#4FA7FF" -fill black -font Calibri-Bold -pointsize 32 -gravity center -annotate +0+0 "Number 2" \) \
+smush +2 \ 
-write mpr:numbers \ 
\
\( 
\( -size 881x488 xc:"#FFE97F" \) \
\( -size 881x488 xc:"#00FF90" \) \ 
-resize 1180x441! +smush +6 mpr:numbers +swap -gravity center -smush +15 +gravity \ 
-write mpr:boxes \ 
-delete 0 \ 
\ 
\( -size 1104x89! xc:"#00137F" -fill white -font Calibri-Bold -pointsize 48 -gravity center -annotate +0+0 "Different boxes" \) \ 
mpr:boxes +swap -gravity center -smush +24 +gravity +write POut.png \) null:

The desired output is like this:

enter link description

Thanks for any help.

UPDATE

When I see it in an image editor (Paint.net in my case) I can see and change resolution withot change pixel dimentions. Only dimentions of inches change.

Result.png original with Resolution=120 pixel/inch and size 1180x606

enter link description

Result.png changed to Resolution=96 pixel/inch and size still is 1180x606 but inches dimentions changed

enter link description

UPDATE 2

fmw42's code works fine creating from scratch 3 images and then merging them. My problem is if I use the same fmw42's script but instead to create the yellow and green boxes I crop them from another image (source.png) the result.png is not the same. What is the issue when I add the cropped images?

I'm using this code:

convert \
source.png +repage -write mpr:img -delete 0--1 \
\( \
\( -size 1104x89! xc:"#00137F" -fill white -font Calibri-Bold -pointsize 48 -gravity center -annotate +0+0 "Different boxes" \) \
\) \
\
\( \
\( -size 426x37 xc:"#4FA7FF" -fill black -font Calibri-Bold -pointsize 32 -gravity center -annotate +0+0 "Number 1" \) \
\( -size 426x37 xc:"#4FA7FF" -fill black -font Calibri-Bold -pointsize 32 -gravity center -annotate +0+0 "Number 2" \) \
+smush +2 \
\) \
-smush +24 \
\
\( \
\( mpr:img -crop 881x488+71+376   \) \
\( mpr:img -crop 881x488+992+376  \) \
+smush +6 -resize 1180x441! \
\) \
-smush +15  \
resultX.png

This is source.png

enter link description

And this is the output that is not correct

enter link description


Solution

  • Does this do what you want? ImageMagick 6 command could be like the following as one way to do it:

    convert \( \( -size 1104x89! xc:"#00137F" -fill white -font Calibri-Bold -pointsize 48 -gravity center -annotate +0+0 "Different boxes" \) \
    -write P3.png  \) \
    \
    \( \( -size 426x37 xc:"#4FA7FF" -fill black -font Calibri-Bold -pointsize 32 -gravity center -annotate +0+0 "Number 1" \) \
    \( -size 426x37 xc:"#4FA7FF" -fill black -font Calibri-Bold -pointsize 32 -gravity center -annotate +0+0 "Number 2" \) \
    +smush +2 -write P1.png \) \
    -smush +24 \
    \
    \( \( -size 881x488 xc:"#FFE97F"  \) \
    \( -size 881x488 xc:"#00FF90" \) \
    +smush +6 -resize 1180x441! -write P2.png \) \
    -smush +15 \
    result.png
    


    enter image description here

    Note that I may not have used the same font as you.

    Please review:

    https://imagemagick.org/Usage/basics/#parenthesis https://imagemagick.org/Usage/layers/#smush https://imagemagick.org/Usage/files/#write

    To answer your Update2 question: You need to resent the gravity with +gravity after you have used it with -gravity center. Also you need to add +repage after your crops.

    convert \
    source.png +repage -write mpr:img -delete 0--1 \
    \( \
    \( -size 1104x89! xc:"#00137F" -fill white -font Calibri-Bold -pointsize 48 -gravity center -annotate +0+0 "Different boxes" \) \
    \) \
    \
    \( \
    \( -size 426x37 xc:"#4FA7FF" -fill black -font Calibri-Bold -pointsize 32 -gravity center -annotate +0+0 "Number 1" \) \
    \( -size 426x37 xc:"#4FA7FF" -fill black -font Calibri-Bold -pointsize 32 -gravity center -annotate +0+0 "Number 2" \) \
    +smush +2 \
    \) \
    -smush +24 +gravity \
    \
    \( \
    \( mpr:img -crop 881x488+71+376 +repage \) \
    \( mpr:img -crop 881x488+992+376 +repage \) \
    +smush +6 -resize 1180x441! \
    \) \
    -gravity center -smush +15  \
    resultX.png
    


    enter image description here