Search code examples
imagemagickimagemagick-convert

Add header text to merged regions ImageMagick


I crop 2 regions from source image to merge them in pairs and then add a header centered at the top that says "Left Right" with red background. For this I have this code that uses the source input.png and LeftRight.png as helper.

This is the Input.png

input.png

This is the helper image I use to create the header LeftRight.png

LeftRight.png

The code so far:

convert input.png \ 
-write mpr:img -delete 0--1 -background none -bordercolor black \ 
\( \( mpr:img -crop 240x151+462+176 \) \   ( mpr:img -crop 240x151+87+257  \) \ 
+smush +6 LeftRight.png +swap -gravity   center -smush +3 +gravity -write Img-A.png    \) -delete 0--1 \ 
\( \( mpr:img -crop 240x151+468+542 \) \( mpr:img -crop 240x151+140+624 \) \ 
+smush +6 LeftRight.png +swap -gravity center -smush +3 +gravity -write Img-B.png \) null:

 I generate this 2 images with the code: 

Img-A.png

Img-B.png

 How can be modified this part     

+smush +6 LeftRight.png +swap -gravity center -smush +3 +gravity -write

 in order to add a header text "Left Rigth" with same characteristics without using the helper image LeftRight.png ?


Solution

  • You should create your text image first in ImageMagick, then save to mpr: to use later. See my 2nd - 4th lines.

    Note your input.png got changed to JPG.

    convert \
    \( -size 159x24 xc:"#7f0000" -fill white -font ubuntu -pointsize 18 -gravity center -annotate +0+0 "LEFT" \) \
    \( -size 159x24 xc:"#7f0000" -fill white -font ubuntu -pointsize 18 -gravity center -annotate +0+0 "RIGHT" \) \
    -background white +smush +2 -write mpr:leftright +delete +gravity \
    input.jpg +repage -write mpr:img -delete 0--1 -background none -bordercolor black \
    \( \( mpr:img -crop 240x151+462+176 +repage -border 2 \) \( mpr:img -crop 240x151+87+257 +repage -border 2 \) \
    +smush +6 mpr:leftright +swap -gravity center -smush +3 +gravity -write Img-A.png \) -delete 0--1 \
    \( \( mpr:img -crop 240x151+468+542 +repage -border 2 \) \( mpr:img -crop 240x151+140+624 +repage -border 2 \) \
    +smush +6 mpr:leftright +swap -gravity center -smush +3 +gravity -write Img-B.png \) null:
    


    enter image description here

    enter image description here

    Your -bordercolor black does nothing here since you have not added -border x. I have included -border 2 to put the border around the cropped images. Also your -delete 0--1 could be replaced here with simply +delete since there is only one image created to be deleted in this example.

    ADDITION:

    You asked to resize the images to 380x150. You can do that as follows. But note that the aspect of the original cropped images are not the same as 380x150. So to get exactly 380x150 without losing data by keeping the aspect ratio or padding, I have to distort the resulting cropped images to fit exactly to 380x150. See where I put -resize 380x150!. The "!" tells ImageMagick to distort the image to fit exactly to your 380x150

    convert \
    \( -size 159x24 xc:"#7f0000" -fill white -font ubuntu -pointsize 18 -gravity center -annotate +0+0 "LEFT" \) \
    \( -size 159x24 xc:"#7f0000" -fill white -font ubuntu -pointsize 18 -gravity center -annotate +0+0 "RIGHT" \) \
    -background white +smush +2 -write mpr:leftright +delete +gravity \
    input.jpg +repage -write mpr:img -delete 0--1 -background none -bordercolor black \
    \( \( mpr:img -crop 240x151+462+176 +repage -resize 380x150! -border 2 \) \( mpr:img -crop 240x151+87+257 +repage -resize 380x150! -border 2 \) \
    +smush +6 mpr:leftright +swap -gravity center -smush +3 +gravity -write Img-A.png \) -delete 0--1 \
    \( \( mpr:img -crop 240x151+468+542 +repage -resize 380x150! -border 2 \) \( mpr:img -crop 240x151+140+624 +repage -resize 380x150! -border 2 \) \
    +smush +6 mpr:leftright +swap -gravity center -smush +3 +gravity -write Img-B.png \) null:
    


    enter image description here

    enter image description here

    ADDITION 2:

    If you want to resize the appended pairs of cropped images, then put the resize right after both +smush 6, which append the two images. Note again the results may be distorted, since I forced it to fit the exact size. If you do not want distortion, then you must specify whether you want the image cropped or padded or just fit to the width or the height.

    convert \
    \( -size 159x24 xc:"#7f0000" -fill white -font ubuntu -pointsize 18 -gravity center -annotate +0+0 "LEFT" \) \
    \( -size 159x24 xc:"#7f0000" -fill white -font ubuntu -pointsize 18 -gravity center -annotate +0+0 "RIGHT" \) \
    -background white +smush +2 -write mpr:leftright +delete +gravity \
    input.jpg +repage -write mpr:img -delete 0--1 -background none -bordercolor black \
    \( \( mpr:img -crop 240x151+462+176 +repage -border 2 \) \( mpr:img -crop 240x151+87+257 +repage -border 2 \) \
    +smush +6 -resize 380x150! mpr:leftright +swap -gravity center -smush +3 +gravity -write Img-A.png \) -delete 0--1 \
    \( \( mpr:img -crop 240x151+468+542 +repage -border 2 \) \( mpr:img -crop 240x151+140+624 +repage -border 2 \) \
    +smush +6 -resize 380x150! mpr:leftright +swap -gravity center -smush +3 +gravity -write Img-B.png \) null:
    


    enter image description here

    enter image description here

    ADDITION 3:

    If you want to resize the two final output images, then put the resize command just before you write those images.

    convert \
    \( -size 159x24 xc:"#7f0000" -fill white -font ubuntu -pointsize 18 -gravity center -annotate +0+0 "LEFT" \) \
    \( -size 159x24 xc:"#7f0000" -fill white -font ubuntu -pointsize 18 -gravity center -annotate +0+0 "RIGHT" \) \
    -background white +smush +2 -write mpr:leftright +delete +gravity \
    input.jpg +repage -write mpr:img -delete 0--1 -background none -bordercolor black \
    \( \( mpr:img -crop 240x151+462+176 +repage -border 2 \) \( mpr:img -crop 240x151+87+257 +repage -border 2 \) \
    +smush +6 mpr:leftright +swap -gravity center -smush +3 +gravity -resize 380x150! -write Img-A.png \) -delete 0--1 \
    \( \( mpr:img -crop 240x151+468+542 +repage -border 2 \) \( mpr:img -crop 240x151+140+624 +repage -border 2 \) \
    +smush +6  mpr:leftright +swap -gravity center -smush +3 +gravity -resize 380x150! -write Img-B.png \) null:
    


    enter image description here

    enter image description here