2016-07-20 68 views
4

我具有一個圖像尺寸720x720logo.png,我希望它被拉伸或擠壓以適合整個高度或寬度,保持縱橫比,集中在另一圖像中的有界長方形top-left: 32,432bottom-right: 607,919background.png圖片尺寸爲640x960插入一個圖像到另一個使用轉換

因此,對於上述示例,logo.png將調整爲488x488並定位在top-left: 76,432

但我不希望有計算488x48876,432,只是想用top-leftbottom-right符以上,即讓ImageMagick的數字出來。

ImageMagick可以這樣做嗎?如果它不能獨立運行,是否有腳本解決方案使用convert以及其他任何東西?

回答

3

希望更簡單的版本

我認爲這仍然產生相同的結果,但更簡單:

#!/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

enter image description here

enter image description here

+1

關於(76,432),假設Y座標432是顯而易見的,X座標是32 + [(608 - 32) - 488]/2 = 76,它應該水平居中。 –

+1

我認爲現在是正確的。 –

+0

謝謝。我改編了你的腳本來使用命令行參數,爲我節省了幾天的工作時間! –