从蒙太奇(ImageMagick)合成图像中删除额外的图块空间?

从蒙太奇(ImageMagick)合成图像中删除额外的图块空间?

我有一堆照片。它们的高度都相同,我试图通过 Bash 使用 ImageMagick 的蒙太奇程序将它们组合成一个合成图像。问题是,默认情况下,图块大小(一张图像占据一个图块)等于最大尺寸的图像。因此,狭窄的图像被大量的空白包围。我想删除这个空白。我该怎么做?

答案1

尝试这样的事情:

montage file1.jpg file2.jpg -geometry +0+0 -background none output.jpg

这将使图像之间的边界尽可能小,并且无论存在什么都将是透明的。

要查看使用内置图像的差异演示,请尝试这些并进行比较:

$ montage rose: -resize 100x60 rose: -geometry +0+0 -background none montage.jpg
$ display montage.jpg &
$ montage rose: -resize 100x60 rose: montage.jpg
$ display montage.jpg &

蒙太奇用法

如果您发布您所获得的示例,并手动编辑您想要的结果示例,我们也许能够更接近这一目标。

以下是我比上面最初发布的示例更喜欢的示例:

montage \( rose: -resize 100x46\! \) rose: -background gray montage.jpg

enter image description here

montage \( rose: -resize 100x46\! \) rose: -geometry +0+0 -background none montage.jpg

enter image description here

答案2

我同意-geometry +0+0删除额外的瓷砖空间的公认答案,并且我会添加-mode Concatenate(在某些条件下)。

另外,一旦你有不同的尺寸montage,就很难区分什么是“平铺背景”(平铺空间)与“框架”和“边框”——我自己经常迷失方向,所以这里有一个小测试用例(可点击)图片:

#$ montage --version # done on:
#Version: ImageMagick 6.6.2-6 2012-08-17 Q16 http://www.imagemagick.org
# pipe to `display` (for preview):
# montage img1.png img3.png img2.png img4.png bmp:- | display

# generate images first
convert -size 200x100 xc:red img1.png
convert -size 300x200 xc:blue img2.png
convert -size 400x300 xc:green img3.png
convert -size 500x400 xc:orange img4.png

# #01: direct montage (-tile 2x2 automatic for four images)
# note: mont01.png is 256x252 pixels!
montage img1.png img3.png img2.png img4.png \
  mont01.png

mont01

# "The 'tile' size is then set to the largest dimentions
# of all the resized images, and the size actually specified."
# "by removing the 'size' component, non of the images will
# be resized, and the 'tile' size will be set to the largest
# dimensions of all the images given"

# #02: specify -geometry offset (tile spacing)
# note: mont02.png is 1008x808 pixels now!
montage img1.png img3.png img2.png img4.png \
  -geometry +2+2 \
  mont02.png

mont02

# #03: add border to command #02:
# border sticks around images themselves
montage img1.png img3.png img2.png img4.png \
  -geometry +2+2 -border 5 \
  mont03.png

mont03

# #04: add frame to command #02:
# frame is around the tile (and is "3D") - and
# background (which isn't image) is colored default gray:
montage img1.png img3.png img2.png img4.png \
  -geometry +2+2 -frame 5 \
  mont04.png

mont04

# #05: add background color spec to command #04:
# that is background behind the tiles - not of the tiles
montage img1.png img3.png img2.png img4.png \
  -geometry +2+2 -frame 5 -background "brown" \
  mont05.png

mont05

# #06: add mattecolor to command #05:
# "-mattecolor The color used as the frame color."
# but just changes color of the "3D" frame borders
montage img1.png img3.png img2.png img4.png \
  -geometry +2+2 -frame 5 -mattecolor "white" -background "brown" \
  mont06.png

mont06

# #07: add bordercolor  to command #05:
# "-bordercolor   The fill color inside the frame for images, or any border padding."
# this does change the color of time background
montage img1.png img3.png img2.png img4.png \
  -geometry +2+2 -frame 5 -bordercolor "purple" -background "brown" \
  mont07.png

mont07

# #08: both frame and border :
# no difference from command #07 -
# once the tiles are resized, the entire remaining
# background is used as a "border", and specifying
# "-border 5" size for it has no effect
montage img1.png img3.png img2.png img4.png \
  -geometry +2+2 -frame 5 -border 5 -bordercolor "purple" \
  mont08.png

mont08

# #09: add mode Concatenate (with -tile) to #08
# No difference from #08
montage img1.png img3.png img2.png img4.png \
  -mode Concatenate -tile 2x2 -geometry +2+2 -frame 5 -border 5 -bordercolor "purple" \
  mont09.png

mont09

# #10 remove -frame, from #09
# now there is no tile background, and
# images are not centered in tiles (they
# have gravity NorthWest instead)
montage img1.png img3.png img2.png img4.png \
  -mode Concatenate -tile 2x2 -geometry +2+2 -border 5 -bordercolor "purple" \
  mont10.png

mont10

# #11 Mode Concatenate with only -tile
# images are without padding (as much as possible)
montage img1.png img3.png img2.png img4.png \
  -mode Concatenate -tile 2x2 -border 5 -bordercolor "purple" \
  mont11.png

mont11

# #12 Try geometry +0+0 instead of concatenate
# almost the same as #11, except more correct overall borders
montage img1.png img3.png img2.png img4.png \
  -tile 2x2 -geometry +0+0 -border 5 -bordercolor "purple" \
  mont12.png

mont12

好吧,希望这能有用,
干杯!


编辑:我为 ImageMagick 制作了一个小型的 Python/Tkinter/PIL GUI,tkGui_ImageMagick.py- 最后,我可以找到适合我想要的东西的命令行:制作四个图像的蒙太奇,其中图块的高度和宽度与该列(或行的高度)的最大宽度相匹配。

在此示例中,img1 (200x100) 和 img2 (300x200) 位于第一列,较大的宽度为 300 - 这应该设置 img1 的平铺宽度。此外,img1 的高度需要与 img3 的较大高度(300 像素)相关,并与之形成一行。这可以通过指定extent运算符(另请参见ImageMagick • 查看主题 - 调整大小并填充而不是拉伸)。该命令行需要子进程调用来分隔montage每列的 s - 并从那里convert为每个图像分隔 s:

montage \
  <(montage \
    <(convert \
      img1.png -gravity center -extent 300x300  \
      bmp:-) \
    <(convert \
      img2.png -gravity North -extent x400  \
      bmp:-) \
  -tile 1x -geometry +0+0  \
  bmp:-) \
  <(montage \
    <(convert \
      img3.png -gravity center -extent 500x  \
      bmp:-) \
    img4.png \
  -tile 1x -geometry +0+0  \
  bmp:-) \
-geometry +0+0 -border 2 \
mont13.png

# or as one liner:

montage <(montage <(convert img1.png -gravity center -extent 300x300  bmp:-) <(convert img2.png -gravity North -extent x400  bmp:-) -tile 1x -geometry +0+0  bmp:-) <(montage <(convert img3.png -gravity center -extent 500x  bmp:-) img4.png -tile 1x -geometry +0+0  bmp:-) -geometry +0+0 -border 2 mont13.png

mont13

-extents这里注意,如果我们直接在蒙太奇行中使用,如下所示:

montage \
    img1.png -extent 300x200 -gravity center \
    img2.png -extent 0x400 -gravity North \
    -tile 1x -geometry +0+0  \
  bmp:-

...我们会注意到,第一个高度规范 (200) 将被忽略,并且较大的 400 将应用于两个图块无论如何

因此,我们必须控制每个单独图像的填充(通过convert调用extentsfor每个) - 然后extentsmontage队列中避开;因此,我们必须先验地知道每列的(最大)宽度(以及每行的高度)。另请注意:

  • 由于 img1 小于其邻居的隐含宽度/高度,因此我们必须在其范围内显式设置宽度和高度
  • convert另一个维度只能具有指​​定的相关维度 - 并且 img4 作为最大的维度,根本不需要填充(并遍历)
  • montage,通常-gravity必须来(详细说明:w & h)-extent;在convert,-gravity作品 -extent(通常)

答案3

不是montage,但感觉更准确的是以下内容:

convert 1.jpg 2.jpg 3.jpg -geometry x500 +append -gravity South a.png

x500是所需的最终高度

相关内容