2016-07-20 19 views
4

Ich habe ein Bild logo.png der Größe 720x720, ich will es über die gesamte Höhe oder Breite passen gedehnt oder gestaucht wird, Seitenverhältnis beibehalten wird, innerhalb eines begrenzten Rechtecks ​​zentrierte top-left: 32,432 und bottom-right: 607,919 in einem anderen Bild background.png Bild der Größe 640x960.Stecken Sie ein Bild in ein anderes mit konvertieren

Für das obige Beispiel würde logo.png auf 488x488 geändert und auf top-left: 76,432 positioniert werden.

Aber ich will nicht zu berechnen 488x488 oder 76,432 haben, wollen einfach nur über die top-left und bottom-right Bezeich verwenden, das heißt lassen ImageMagick es herausfinden.

Kann ImageMagick so etwas tun? Wenn es nicht alleine möglich ist, gibt es eine Skriptlösung, die convert und alles andere verwendet?

Antwort

3

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 

enter image description here

enter image description here

enter image description here

+1

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. –

+1

Ich denke, es ist jetzt richtig. –

+0

Danke. Ich habe Ihr Skript angepasst, um Befehlszeilenargumente zu verwenden, und habe mir die Arbeitstage gerettet! –

Verwandte Themen