希望更簡單的版本
我認爲這仍然產生相同的結果,但更簡單:
#!/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
![enter image description here](https://i.stack.imgur.com/sYFoJ.png)
![enter image description here](https://i.stack.imgur.com/uipK2.png)
![enter image description here](https://i.stack.imgur.com/eUQbg.png)
關於(76,432),假設Y座標432是顯而易見的,X座標是32 + [(608 - 32) - 488]/2 = 76,它應該水平居中。 –
我認爲現在是正確的。 –
謝謝。我改編了你的腳本來使用命令行參數,爲我節省了幾天的工作時間! –