This post sprung out of a discussion between @Morgan_Hardwood and myself on IRC. I’m making notes here for posterity’s sake.
We were looking to annotate an image (using imagemagick) by including an extended area below the image that would print a logo, title, photographer across the area:
The only thing we knew for sure was that we wanted the added bar to be 40px high. This would be part of a bash script to cycle through all the images in a directory and add this information - so the width is highly variable.
We tried a couple of methods before settling on this rough working idea:
-
Open the image.
-
Clone the image.
a. fill the clone with a solid color
b. resize the clone to 40px tall (keeping the original image width, whatever it is)
c. add annotations and logos to the resized bar (so parameters like gravity are relative to the bar - we can vertically center things on the bar easily). -
-append
the bar to the bottom of the original image. -
Output
Here is the full command we (finally) used:
magick convert mairi-mid-air.jpg \
\( +clone \
-fill gray \
-draw 'color 0,0 reset' \
-resize x40! \
-fill white \
-pointsize 14 \
-gravity center \
-annotate 0 'Mairi Mid-Air' \
-gravity east \
-annotate +10+0 'Pat David' \
-gravity west \
\( PatDavid-League-Raleway-white.png -resize x35 \) \
-geometry +2.5+0 -composite \) \
-append out.jpg
Let’s see what madness we had wrought…
Making the Bar
magick convert mairi-mid-air.jpg
\( +clone
This loads the image in question, and creates a clone of it.
We are going to leverage the fact that the clone is the same dimensions (width) of the original image, resize it to our bar height, and do all operations against this bar. A the end we’ll append it to the bottom of the original before saving. (Notice all the subsequent operations take place inside the outer-most parenthesis - the final step -append
will combine everything we do inside the parens to the original image at the bottom).
-fill gray
-draw 'color 0,0 reset'
-resize x40!
Set the intended bar fill color to be “gray”.
Fill the cloned image with the fill color.
Resize to 40px high (ignore aspect ratio to keep the same width).
Set Font Color + Size
-fill white
-pointsize 14
Set the font color to be “white”, and set the font size to be 14 points.
Set the Title (Center)
-gravity center
-annotate 0 'Mairi Mid-Air'
In the center of the bar, add the title text.
Set the Author (Right)
-gravity east
-annotate +10+0 'Pat David'
On the right side of the bar, set the name. The +10+0
on the -annotate
parameter is to shift this text relative to the right side of the image (pushing it in 10px).
Add the Logo (Left)
On the left side of the bar,
-gravity west
load the logo (and resize it to 35px high),
\( PatDavid-League-Raleway-white.png -resize x35 \)
Add it to the bar and shift it 2.5px from the edge.
-geometry +2.5+0 -composite \)
Finally!
And finally, use the -append
command to vertically append our result onto the original base image.