我有一个logo.png
尺寸为image的图像720x720
,我希望将其拉伸或挤压以适合整个高度或宽度,并保留宽高比,并以一个有边界的矩形居中,top-left: 32,432
并bottom-right: 607,919
在另一个background.png
尺寸为size的图像中居中640x960
。
因此,对于上述示例,logo.png
将其调整为488x488
并定位在top-left: 76,432
。
但是我不需要计算488x488
或76,432
,只想使用上面的top-left
和bottom-right
指定符,即让ImageMagick弄清楚。
ImageMagick可以做这样的事情吗?如果不能单独使用,是否有使用convert
其他方法的脚本解决方案?
希望更简单的版本
我认为这仍然会产生相同的结果,但更简单:
#!/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