Search code examples
imagemagickimagemagick-convert

Positioning layers


At the moment I'm working on a quite comprehensive (well at least for me) imagemagic task. I want to add some annotations i extract from exif-data and adding an overlay image. At the moment i'm having the following code:

convert -verbose source.jpg \
    -pointsize 32 -fill white -undercolor '#00000070' -gravity NorthWest -annotate +10+10 "some exif data"\
    -pointsize 32 -fill white -undercolor '#00000070' -gravity North     -annotate +10+10 "more exif data"\
    -pointsize 32 -fill white -undercolor '#00000070' -gravity NorthEast -annotate +10+10 "even more exif data"\
    -gravity South overlay.png \
    -layers flatten \
    -quality 95 destination.jpg;

The annotation works fine including the position. But i stuck with the overlay. It seems the the "-gravity South" doesn't work here. It stuck on the upper left corner. What do i have to change to get the overlay to the bottom and centered?

Bonus question: How to get the overlay semitransparent?


Solution

  • You should use -composite rather than -flatten with your convert:

    convert -verbose source.jpg \
        -pointsize 32 -fill white -undercolor '#00000070' -gravity NorthWest -annotate +10+10 "some exif data"\
        -pointsize 32 -fill white -undercolor '#00000070' -gravity North     -annotate +10+10 "more exif data"\
        -pointsize 32 -fill white -undercolor '#00000070' -gravity NorthEast -annotate +10+10 "even more exif data"\
        -gravity South overlay.png -composite \
        result.jpg
    

    enter image description here

    If you want the overlay semi-transparent, use:

    convert -verbose source.jpg \
        -pointsize 32 -fill white -undercolor '#00000070' -gravity NorthWest -annotate +10+10 "some exif data"\
        -pointsize 32 -fill white -undercolor '#00000070' -gravity North     -annotate +10+10 "more exif data"\
        -pointsize 32 -fill white -undercolor '#00000070' -gravity NorthEast -annotate +10+10 "even more exif data"\
        \( -gravity South overlay.png -channel A -fx "0.5" \) -composite \
        result.jpg
    

    By the way, -pointsize, -fill, and -undercolor are "settings", so they remain set until changed, so you don't need to repeat them:

    convert -verbose source.jpg -pointsize 32 -fill white -undercolor '#00000070' \
        -gravity NorthWest -annotate +10+10 "some exif data"\
        -gravity North     -annotate +10+10 "more exif data"\
        -gravity NorthEast -annotate +10+10 "even more exif data"\
        \( -gravity South overlay.png -channel A -fx "0.5" \) -composite \
        result.jpg