ImageMagick使用文件中的边框颜色构图

时间:2017-09-05 08:21:00

标签: imagemagick

我正在尝试为边框添加框架/边框" color"来自一个文件。 该文件可在此处找到https://storage.googleapis.com/ramki-opticlan/paspartu_small.jpg

我认为Imagemagick无法做到这一点,所以我的想法是创建一个具有所需尺寸的新背景并将图像放在它上面。

#creates the background
convert -size ${PICTURE_WIDTH}x${PICTURE_HEIGHT} tile:paspartu_small.jpg background.jpg

# places one on top of each other
convert background.jpg picture_that_needs_to_be_framed.jpg -gravity center -region +0-0 -composite -matte result.jpg

我的问题是我无法计算背景的尺寸并正确调整图像位置而不会破坏图像宽高比。 如果有人可以帮我创建一个脚本来计算这些位置,我将非常感激。

让我们以此为例:

  • 我的照片是600x500px
  • 上边框必须为40px
  • 右边框必须为50px
  • 底部边框必须为60px
  • 左边框必须为50px

希望,我想要实现的目标是可以理解的。 干杯

我正在使用IM7

1 个答案:

答案 0 :(得分:2)

如果你有ImageMagick v7 +,你可以这样做:

#!/bin/bash
TOP=40
RIGHT=50
BOTTOM=60
LEFT=50
PICTURE="artwork.jpg"
BACKGROUND="background.jpg"

magick "$PICTURE" \
       -size "%[fx:w+${LEFT}+${RIGHT}]"x"%[fx:h+${TOP}+${BOTTOM}]" tile:"$BACKGROUND" \
       +swap -geometry +${LEFT}+${TOP} -composite result.jpg

如果你有ImageMagick v6,这应该做你想要的,并且很容易定制:

#!/bin/bash
TOP=40
RIGHT=50
BOTTOM=60
LEFT=50
PICTURE="artwork.jpg"
BACKGROUND="background.jpg"

# Get width and height of picture
read w h < <(convert "$PICTURE" -format "%w %h" info:)

((NEW_W=w+LEFT+RIGHT))
((NEW_H=h+TOP+BOTTOM))

convert -size ${NEW_W}x${NEW_H} tile:"$BACKGROUND" \
        "$PICTURE" -geometry +${LEFT}+${TOP} -composite result.jpg

所以,如果我从这开始:

enter image description here

我最终得到了这个:

enter image description here

相关问题