Search code examples
textheaderimagemagick-convert

Modify code to add top header ImageMagick


Please some help here. I´m using IM 6.9.10-11 Q16 x86_64

The following coordinates correlative are related with the box number in input image. The first 3 lines in convert command create a header text "Left - Right" that goes over the img2.png and img3.png

coord1="98x35+234+16" 
coord2="178x59+29+65"
coord3="178x59+223+65"
coord4="178x59+417+65"
coord5="178x59+611+65"
coord6="239x147+35+179" 
coord7="239x147+416+179"
coord8="239x147+73+349"
coord9="239x147+401+353"    
   
convert \
\( -size 125x36 xc:"#00137F" -fill white -font Calibri-Bold -pointsize 32 -gravity center -annotate +0+0 "Left" \) \
\( -size 125x36 xc:"#00137F" -fill white -font Calibri-Bold -pointsize 32 -gravity center -annotate +0+0 "Right" \) \
 -background white +smush +2 -write mpr:leftright +delete +gravity \
 \
input.png +repage -write mpr:img -delete 0--1 -background none -bordercolor "#870000"  \
\( mpr:img -crop $coord2 \)  \
\( mpr:img -crop $coord3 \)  \
\( mpr:img -crop $coord4 \)  \
\( mpr:img -crop $coord5 \)  \
-border 4 \( -clone 0,1 -smush -4 \) \( -clone 2,3 -smush -4 \) -delete 0-3 -gravity center +smush -4 +gravity \
\( mpr:img -crop $coord1 -border 4 \) +swap -gravity center -smush +12 +gravity -write Img1.png \
 \
\( \( mpr:img -crop $coord6 +repage -border 4 \) \( mpr:img -crop $coord7 +repage -border 4 \) \
   +smush +6 mpr:leftright +swap -gravity center -smush +11 +gravity  -write Img2.png  \) -delete 0--1 \
\
\( \( mpr:img -crop $coord8 +repage -border 4 \) \( mpr:img -crop $coord9 +repage -border 4 \) \
   +smush +6 mpr:leftright +swap -gravity center -smush +11 +gravity  -write Img3.png  \) null:  

With this input.png

input.png

enter link description here

The code produces this 3 images.

Img1.png

enter link description here

Img2.png

enter link description here

Img3.png

enter link description here

What I´d like is to modify the current code to add a top header text to each one of these 3 images before they be created (before -write) In order that the Img1, Img2 and Img3 be finally like this:

Desired Img1

enter link description here

Desired Img2

enter link description here

Desired Img3

enter link description here

I think I need to create the images first at the beginning of the convert command, something like this, but I don´t know how to adapt it and insert these lines within the current code.

\( -size 500x36 xc:"#757575" -fill white -font Calibri-Bold -pointsize 32 -gravity center -annotate +0+0 "Five boxes" \) \
\( -size 500x36 xc:"#757575" -fill white -font Calibri-Bold -pointsize 32 -gravity center -annotate +0+0 "Blue boxes" \) \
\( -size 500x36 xc:"#757575" -fill white -font Calibri-Bold -pointsize 32 -gravity center -annotate +0+0 "Green boxes" \) \

Thanks for any help.


Solution

  • In ImageMagick you can create images anywhere in parenthesis and reposition them with -swap ... (or +swap for the last two). So your command would become:

    coord1="98x35+234+16" 
    coord2="178x59+29+65"
    coord3="178x59+223+65"
    coord4="178x59+417+65"
    coord5="178x59+611+65"
    coord6="239x147+35+179" 
    coord7="239x147+416+179"
    coord8="239x147+73+349"
    coord9="239x147+401+353"    
    
    convert \
    \( -size 125x36 xc:"#00137F" -fill white -font Calibri-Bold.ttf -pointsize 32 -gravity center -annotate +0+0 "Left" \) \
    \( -size 125x36 xc:"#00137F" -fill white -font Calibri-Bold.ttf -pointsize 32 -gravity center -annotate +0+0 "Right" \) \
    -background white +smush +2 -write mpr:leftright +delete +gravity \
    \
    input.png +repage -write mpr:img -delete 0--1 -background none -bordercolor "#870000"  \
    \( mpr:img -crop $coord2 \)  \
    \( mpr:img -crop $coord3 \)  \
    \( mpr:img -crop $coord4 \)  \
    \( mpr:img -crop $coord5 \)  \
    -border 4 \( -clone 0,1 -smush -4 \) \( -clone 2,3 -smush -4 \) -delete 0-3 -gravity center +smush -4 +gravity \
    \( mpr:img -crop $coord1 -border 4 \) +swap -gravity center -smush +12 +gravity \
    \( -size 250x36 xc:gray -fill white -font Calibri-Bold.ttf -pointsize 32 -gravity center -annotate +0+0 "Five Boxes" \) \
    +swap -background none -gravity center -smush +20 +gravity -write Img1.png \
    \
    \( \( mpr:img -crop $coord6 +repage -border 4 \) \( mpr:img -crop $coord7 +repage -border 4 \) \
    +smush +6 mpr:leftright +swap -gravity center -smush +11 +gravity  \
    \( -size 250x36 xc:gray -fill white -font Calibri-Bold.ttf -pointsize 32 -gravity center -annotate +0+0 "Blue Boxes" \) \
    +swap -background none -gravity center -smush +20 +gravity -write Img2.png  \) -delete 0--1 \
    \
    \( \( mpr:img -crop $coord8 +repage -border 4 \) \( mpr:img -crop $coord9 +repage -border 4 \) \
    +smush +6 mpr:leftright +swap -gravity center -smush +11 +gravity  \
    \( -size 250x36 xc:gray -fill white -font Calibri-Bold.ttf -pointsize 32 -gravity center -annotate +0+0 "Green Boxes" \) \
    +swap -background none -gravity center -smush +20 +gravity -write Img3.png  \) null:  
    


    And the results are:

    enter image description here

    enter image description here

    enter image description here

    Unfortunately, in IM 6 there is no way to know how wide your smushed colored images will be so as to make the title the same width unless you compute that ahead of time. In IM 7, it can be done in-line in your command line. So here I made it the same width as the two left right smushed images. You can change the width by changing the width value in -size 250x36.