【问题标题】:ImageMagick, insert an image at a specific position / size into a larger image?ImageMagick,将特定位置/大小的图像插入更大的图像?
【发布时间】:2017-10-21 10:56:19
【问题描述】:

我首先绘制了一个带有列的宽透明图像,效果很好。

convert -size 5568x1920 xc:none iphone6plus.png
convert iphone6plus.png -strokewidth 0 -fill lightgray -draw "rectangle 1080,0 1121,1920 " iphone6plus.png
convert iphone6plus.png -strokewidth 0 -fill lightgray -draw "rectangle 2202,0 2243,1920 " iphone6plus.png
convert iphone6plus.png -strokewidth 0 -fill lightgray -draw "rectangle 3324,0 3365,1920 " iphone6plus.png
convert iphone6plus.png -strokewidth 0 -fill lightgray -draw "rectangle 4446,0 4487,1920 " iphone6plus.png

然后我正在旋转另一个图像,再次正常工作。

convert /en-US/iPhone6Plus-main_start_multi_child.png \
-rotate -2 \
iphone6plus-new.png

但是,我正在尝试将第二个(旋转图像)插入到特定位置/大小的第一个图像中。我遇到了问题,我尝试了几件事,我得到的最接近的似乎覆盖了源图像。

convert iphone6plus.png \
 -geometry 40x40+5+10  \
 -composite \
iphone6plus-new.png

我应该使用什么?

还有我应该如何提高这个操作的速度。

编辑:问题如下所示...

convert -size 5568x1920 xc:none -strokewidth 0 -fill lightgray \
   -draw "rectangle 1080,0 1121,1920 "  \
   -draw "rectangle 2202,0 2243,1920 "  \
   -draw "rectangle 3324,0 3365,1920 "  \
   -draw "rectangle 4446,0 4487,1920 "  \
   \( iPhone6Plus-main_start_multi_child.png -background transparent -rotate -2 -gravity center -resize 1473x755 \)  \
   -geometry -190-50 \
   \( iPhone6Plus-test_multi_child.png -background transparent -gravity center -resize 1473x755 \)  \
   -geometry +2000-50 \
   -composite iphone6plus-new.png

convert -size 5568x1920 xc:none -strokewidth 0 -fill lightgray \
   -draw "rectangle 1080,0 1121,1920 "  \
   -draw "rectangle 2202,0 2243,1920 "  \
   -draw "rectangle 3324,0 3365,1920 "  \
   -draw "rectangle 4446,0 4487,1920 "  \
   \( iPhone6Plus-test_multi_child.png -background transparent -gravity center -resize 1473x755 \)  \
   -geometry +2000-50 \
   -composite iphone6plus-new.png

【问题讨论】:

    标签: imagemagick imagemagick.net


    【解决方案1】:

    首先,settingsstrokewidthfill 一直存在直到更改,所以不要一直重复它们。

    其次,只需创建一次背景并继续添加,而不是保存并关闭然后在下一行重新打开。

    convert -size 5568x1920 xc:none -strokewidth 0 -fill lightgray \
       -draw "rectangle 1080,0 1121,1920 "  \
       -draw "rectangle 2202,0 2243,1920 "  \
       -draw "rectangle 3324,0 3365,1920 "  \
       -draw "rectangle 4446,0 4487,1920 "    basic.png
    

    现在,加载需要旋转的新图像并在括号内对其应用旋转,以便其余部分不受影响,然后将其合成到背景上:

    convert -size 5568x1920 xc:none -strokewidth 0 -fill lightgray \
       -draw "rectangle 1080,0 1121,1920 "  \
       -draw "rectangle 2202,0 2243,1920 "  \
       -draw "rectangle 3324,0 3365,1920 "  \
       -draw "rectangle 4446,0 4487,1920 "  \
       \( SomeOtherImage.png -rotate -2 -resize AxB \)  \
       -geometry +X+Y -composite result.png
    

    -resize AxB 之后可能需要+repage

    更新答案

    这是否更接近您的需求?

    #!/bin/bash
    convert -size 5568x1920 xc:none -strokewidth 0 -fill lightgray -background transparent -gravity center \
       -draw "rectangle 1080,0 1121,1920"  \
       -draw "rectangle 2202,0 2243,1920"  \
       -draw "rectangle 3324,0 3365,1920"  \
       -draw "rectangle 4446,0 4487,1920"  \
       \( xc:blue[1024x768] -rotate -2 -resize 1473x755 \) -geometry -190-50 -composite \
       \( xc:red[1024x768] -resize 1473x755 \)  -geometry +2000-50 -composite result.png
    

    【讨论】:

    • 当您旋转图像时,其大小和边界框会发生变化。当您尝试在背景图像上合成它时,它是在合成边界框的左上角相对于背景图像的左上角。避免此类问题的最佳方法是使用 -gravity center 和 -geometry +X+Y 以及相对于背景图像中心的 X 和 Y 值。这样,合成将把覆盖图像的中心定位在距背景图像中心所需的偏移点处。因此,将 -gravity center 添加到 Marks 命令和适当的 center rel 中。 X 和 Y。
    • @fmw42 谢谢,但这并不能完全解决我的第二个图像问题。如果我注释掉第一个旋转图像,如标记答案中所示,第二个图像出现在第一个图像的位置。我在我的问题中添加了关于的编辑。
    • 您能否更清楚地解释一下问题所在 - 我目前还不太了解。我所能看到的只是两段新代码和两张新图像,但我不知道哪个是错的还是对的,以及它应该是什么样子....
    • 对它进行了排序:) 添加复合材料是有意义的,它是黑色的。干杯。
    猜你喜欢
    • 2013-08-09
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多