使用ImageMagick自动调整大小覆盖两个图像 [英] Overlaying two images with automatic resize using ImageMagick
问题描述
使用ImageMagick覆盖图像时,是否可以根据背景大小自动调整叠加图像的大小?我现在使用以下代码:
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:
复合overlay.jpeg background.jpeg result.jpeg
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?
推荐答案
首先,叠加和背景不需要相同的复合尺寸上班。例如,给出这两个图像:
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):
sydney.png (352x288):
jet2.png(128x129):
jet2.png (128x129):
尝试以下命令:
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
指定输出图像尺寸 -
-geometry
指定前景的尺寸和位置 -size
specifies the output image dimensions-geometry
specifies the dimensions and location of the foreground
这是我得到的第一个命令:
This is what I get for the first command:
编辑
这是一个bash脚本来完成所有操作你在一个line:
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"
这篇关于使用ImageMagick自动调整大小覆盖两个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!