1

I want to be able to overlay text in the margin of images with varying dimensions. Images of varying dimensions will have a black margin that is 30% the width of the image.

I have the following bash script:

#!/bin/bash

command -v convert >/dev/null 2>&1 || { echo >&2 "I require convert, but it's not installed. aborting!";exit 1;}
command -v identify >/dev/null 2>&1 || { echo >&2 "I require identify, but it's not installed. aborting!";exit 1;}
command -v bc >/dev/null 2>&1 || { echo >&2 "I require bc, but it's not installed. aborting!";exit 1;}

basedir="$(dirname "$(readlink -f "${1}")")"
cd "$basedir"

echo "Please enter your caption and press enter"
read caption

if [ -z "$caption" ]; then
    printf "no caption was selected, aborting!\n"
    exit 1
fi
printf "caption is $caption\n"
if [ ! -d "$basedir"/bakups ]; then
    mkdir -p "$basedir"/bakups
fi
while [ $# -gt 0 ]; do
    file="$1"
    if [ -s "$file" ]; then
        cp -f "$file" bakups
        export imagesize=$(identify -format "%w,%h" "$file")
        export imagewidth=$(echo "$imagesize" | cut -f1 -d",")
        export imageheight=$(echo "$(echo "$imagesize" | cut -f2 -d",")*0.05" | bc)
        convert -background "#0008" -font /usr/share/fonts/truetype/fonts-georgewilliams/CaslonRoman.ttf -pointsize 80 -fill white -gravity east \
        -size $(echo $imagewidth)x$(echo $imageheight) caption:"$caption" \
        "$file" +swap -gravity east -composite "$file" && \
        printf "\n$file watermarked successfully\n"
    fi
    shift
done

It gives me this result:

enter image description here

The text extends past the black margin.

I want the text to stay within the margin, and I want removed that shaded horizontal bar behind the text.

I could tweak the bash script and change the pointsize so that the text stays within the margin, but I want this accomplished automatically without my having to tweak the bash script for images of varying dimensions.

I want the text/margin placement to look like this example below:

enter image description here

1

I am not an imagemagick expert, but I found it easier to first create the black part using caption: which will do text wrapping for you. Then add the picture of the car (without the black part). I hope this will be enough for you to progress towards the desired solution.

As a starting point, I manually cropped the picture to the car alone, in 3WL51.jpg, size 1650x1232, and used 500x1232 as the desired additional black part.

caption="1925 Ford Model A Touring Car

*

Front Engine Rear Wheel Drive"
font=/usr/share/fonts/liberation/LiberationSans-Regular.ttf
file=3WL51.jpg

convert -size 500x1232 -font "$font" \
 -pointsize 80 -background "#0008" -fill white \
 -gravity center  caption:"$caption" \
 "$file" +swap +append out.jpg

car with caption

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.