我有一张大小为logo.png
的{{1}}图像,我希望它能够被拉伸或挤压以适应整个高度或宽度,保留纵横比,居中于有界矩形720x720
内和top-left: 32,432
在另一张bottom-right: 607,919
尺寸为background.png
的图片中。{/ p>
因此,对于上面的示例,640x960
会调整为logo.png
,并定位在488x488
。
但我不想计算top-left: 76,432
或488x488
,只想使用上面的76,432
和top-left
说明符,即让ImageMagick弄明白
ImageMagick可以这样做吗?如果它本身不能,是否有使用bottom-right
和其他任何内容的脚本解决方案?
答案 0 :(得分:5)
希望更简单的版本
我认为这仍然会产生相同的结果,但更简单:
#!/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