定位图层

时间:2017-09-04 23:57:32

标签: imagemagick imagemagick-convert

目前,我正在开展一项非常全面的(至少对我而言)图像作业。我想添加一些我从exif数据中提取的注释并添加叠加图像。 目前我有以下代码:

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;

注释工作正常,包括位置。但我坚持使用叠加层。它似乎是" -gravity South"在这里工作不起作用。它卡在左上角。我需要更改什么才能将叠加层置于底部并居中?

奖金问题:如何让叠加半透明?

2 个答案:

答案 0 :(得分:2)

您应该-composite使用-flatten而不是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

如果您希望叠加层是半透明的,请使用:

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

顺便说一下,-pointsize-fill-undercolor“settings”,因此它们会保持设置直到更改为止,因此您不需要重复一遍:

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

答案 1 :(得分:1)

您需要使用composite而不是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"\
    -layers flatten \
    -quality 95 destination.jpg;

然后:

composite -gravity South overlay.png destination.jpg result.jpg

有关composite here的更多信息。

编辑:最好的方法是使用ImageMagick中间保存格式miff和流水线操作。这将节省磁盘读/写的额外步骤。

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"\
        -layers flatten \
        -quality 95 miff:- | \

composite -gravity South overlay.png miff:- destination.jpg