【问题标题】:Insert one image into another using convert使用 convert 将一张图像插入另一张图像
【发布时间】:2016-11-23 10:15:08
【问题描述】:

我有一个大小为720x720 的图像logo.png,我希望它被拉伸或挤压以适应整个高度或宽度,保持纵横比,在有界矩形top-left: 32,432bottom-right: 607,919 内居中图片background.png 大小为640x960

因此,对于上面的示例,logo.png 的大小将调整为 488x488 并定位在 top-left: 76,432

但我不想计算488x48876,432,只想使用上面的top-leftbottom-right 说明符,即让ImageMagick 算出来。

ImageMagick 可以做这样的事情吗?如果它自己不能,是否有使用convert 和其他任何东西的脚本解决方案?

【问题讨论】:

    标签: imagemagick image-manipulation


    【解决方案1】:

    希望更简单的版本

    我认为这仍然会产生相同的结果,但更简单:

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

    原答案

    我认为,从您的 cmets 来看,您希望调整大小的图像居中,所以我已经这样做了。

    另外,还有很多调试代码,直到我知道我在正确的轨道上之前我还没有优化它,所以它肯定可以改进。

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

    【讨论】:

    • 关于 (76, 432),假设 Y 坐标 432 是明显的,那么 X 坐标是 32 + [(608 - 32) - 488]/2 = 76 应该水平居中。
    • 谢谢。我调整了您的脚本以使用命令行参数,为我节省了几天的工作时间!
    • 太棒了!我很高兴它为您解决了 - 并感谢您提供的非常慷慨的奖励积分。祝你的项目好运!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 2014-07-07
    • 2011-01-29
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多