使用转换将一张图片插入另一张图片

Insert one image into another using convert

我有一张 logo.png 大小 720x720 的图像,我希望将其拉伸或压缩以适合整个高度或宽度,保持纵横比,在有界矩形内居中 top-left: 32,432bottom-right: 607,919 在另一个图像中 background.png 图像大小 640x960.

因此对于上面的示例,logo.png 将调整为 488x488 并定位在 top-left: 76,432

但我不想计算 488x48876,432,只想使用上面的 top-leftbottom-right 说明符,即让 ImageMagick 图出来了。

ImageMagick 可以做这样的事情吗?如果它自己不能,是否有使用 convert 和其他任何东西的脚本解决方案?

希望版本更简单

我认为这仍然会产生相同的结果,但更简单:

#!/bin/bash

# Make initial images
convert -size 720x720! gradient:red-yellow -fill white -gravity center -pointsize 72 -annotate 0 "logo" logo.png
convert -size 640x960! gradient:blue-cyan  -fill white -gravity north  -pointsize 72 -annotate 0 "background" background.png

# Specify top-left and bottom-right
tl="32,432"
br="607,919"

# Get x1,y1,x2,y2 - bounding box of inserted image
IFS=, read -r x1 y1 <<< "$tl"
IFS=, read -r x2 y2 <<< "$br"

# Work out width and height 
w=$((x2-x1+1))
h=$((y2-y1+1))

# Resize logo proportionally, then extend canvas with invisible pixels to full size of insertion area and composite onto background
convert background.png \( logo.png -resize ${w}x${h} -background none -gravity center -extent ${w}x${h} \) -gravity northwest -geometry +${x1}+${y1} -composite result.png

改进答案

这更简单、更快,希望结果相同:

#!/bin/bash

# Make initial images
convert -size 720x720! gradient:red-yellow -fill white -gravity center -pointsize 72 -annotate 0 "logo" logo.png
convert -size 640x960! gradient:blue-cyan  -fill white -gravity north  -pointsize 72 -annotate 0 "background" background.png

# Specify top-left and bottom-right
tl="32,432"
br="607,919"

# Get x1,y1,x2,y2 - bounding box of inserted image
IFS=, read -r x1 y1 <<< "$tl"
IFS=, read -r x2 y2 <<< "$br"

# Work out w and h, and smaller side "s"
w=$((x2-x1+1))
h=$((y2-y1+1))
s=$w
[ $h -lt $w ] && s=$h
echo Smaller side: $s

# Resize logo proportionally, then extend canvas with invisible pixels to full size of insertion area and place on background
convert background.png \( logo.png -resize ${s}x${s} -background none -gravity center -extent ${w}x${h} \) -gravity northwest -geometry +${x1}+${y1} -composite result.png

原答案

我认为,根据您的评论,您希望调整后的图像居中,所以我已经做到了。

此外,还有很多调试代码,我还没有优化这个,直到我知道我在正确的轨道上,所以它肯定可以改进。

#!/bin/bash

# Make initial images
convert -size 720x720! gradient:red-yellow -fill white -gravity center -pointsize 72 -annotate 0 "logo" logo.png
convert -size 640x960! gradient:blue-cyan  -fill white -gravity north  -pointsize 72 -annotate 0 "background" background.png

# Specify top-left and bottom-right
tl="32,432"
br="607,919"

# Get x1,y1,x2,y2 - bounding box of inserted image
IFS=, read -r x1 y1 <<< "$tl"
IFS=, read -r x2 y2 <<< "$br"

# Work out w and h, and smaller side "s"
w=$((x2-x1+1))
h=$((y2-y1+1))
s=$w
[ $h -lt $w ] && s=$h
echo Smaller side: $s

# Work out size of resized image
read -r a b < <(convert logo.png -resize ${s}x${s} -format "%w %h" info:)
echo Resized logo: $a x $b

# Work out top-left "x" and "y"
x=$((x1+((w-a)/2)))
y=$((y1+((h-b)/2)))
echo x:$x, y:$y
convert background.png \( logo.png -resize ${s}x${s} +repage \) -geometry +${x}+${y} -composite result.png