我有一个大小为logo.png的720x720图像,我希望它被拉伸或压缩,以适应整个高度或宽度,保持高宽比,在一个有界的矩形top-left: 32,432和bottom-right: 607,919中,在另一个640x960大小的图像background.png图像中。
因此,对于上面的示例,logo.png将被调整为488x488,并定位于top-left: 76,432。
但我不想计算488x488或76,432,只想使用上面的top-left和bottom-right说明符,即让ImageMagick来计算出来。
ImageMagick能做这样的事情吗?如果它不能单独使用,是否有使用convert和其他任何东西的脚本解决方案?
发布于 2016-07-22 13:53:27
希望更简单的版本
我认为这仍然会产生同样的结果,但更简单:
#!/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



https://stackoverflow.com/questions/38476804
复制相似问题