Hoffentlich noch einfachere Version
Ich denke, das immer noch das gleiche Ergebnis erzeugt, ist aber einfacher:
#!/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
Verbesserte Antwort
Das ist einfacher und schneller und hoffentlich die gleiche Ergebnis:
#!/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
Original-Antwort
Ich denke, von Ihren Kommentaren, die Sie die Größe geänderte Bild zentriert wollen, so habe ich das getan.
Außerdem gibt es viele Debug-Code und ich habe noch nicht optimiert, bis ich weiß, dass ich auf dem richtigen Weg bin, so dass es definitiv verbessert werden kann.
#!/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
Bezug (76, 432), unter der Annahme, Y-Koordinate 432 offensichtlich ist, die X-Koordinate 32 + [(608 - 32) - 488]/2 = 76, die es horizontal zentrieren sollte. –
Ich denke, es ist jetzt richtig. –
Danke. Ich habe Ihr Skript angepasst, um Befehlszeilenargumente zu verwenden, und habe mir die Arbeitstage gerettet! –