Overlaying two images with automatic resize using ImageMagick

拈花ヽ惹草 提交于 2019-11-29 06:55:07

问题


Is there are way to automatically resize the overlay image according to background size when overlaying images using ImageMagick? I am using the following code now:

composite overlay.jpeg background.jpeg result.jpeg

The problem is that sometimes overlay and background are of different sizes, and I'd like to resize overlay accordingly (keeping the aspect ratio) and place it to the center. Is there any way to do that?


回答1:


First of all, overlay and background do not need to be the same size for composite to work. For example, given these two images:

sydney.png (352x288):

jet2.png (128x129):

Try the following commands:

convert -size 352x288 -composite sydney.png jet2.png -geometry 64x64+176+144 -depth 8 test.png

convert -size 352x288 -composite sydney.png jet2.png -geometry 32x32+176+144 -depth 8 test.png
  • -size specifies the output image dimensions
  • -geometry specifies the dimensions and location of the foreground

This is what I get for the first command:

EDIT

Here's a bash script to do it all for you in one line:

#!/bin/bash
if [ -z "$3" ]
then
    echo "usage: $0 background.png foreground.png output.png"
    exit 1
fi
bg_size=`identify -format '%wx%h' "$1"`
convert -size $bg_size -composite "$1" "$2" -geometry $bg_size+0+0 -depth 8 "$3"


来源:https://stackoverflow.com/questions/4802153/overlaying-two-images-with-automatic-resize-using-imagemagick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!