22 bash imagemagick image-manipulation
我有一堆照片。它们的高度都相同,我试图通过 Bash 使用 ImageMagick 的蒙太奇程序将它们组合成一个合成图像。问题是,默认情况下,tile 大小(一个图像占用一个 tile)等于具有最大尺寸的图像。因此,窄图像被大量空白包围。我想删除这个空白区域。我该怎么做?
Den*_*son 17
尝试这样的事情:
montage file1.jpg file2.jpg -geometry +0+0 -background none output.jpg
Run Code Online (Sandbox Code Playgroud)
这将使图像之间的边界尽可能小,并且任何内容都是透明的。
要查看使用内置图像的差异演示,请尝试这些并进行比较:
$ 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 &
Run Code Online (Sandbox Code Playgroud)
请参阅蒙太奇用法。
如果您发布您所获得的示例并手动编辑您想要的示例,我们可能会更接近这一点。
以下是我比我最初在上面发布的那些更喜欢的示例:
montage \( rose: -resize 100x46\! \) rose: -background gray montage.jpg
Run Code Online (Sandbox Code Playgroud)
montage \( rose: -resize 100x46\! \) rose: -geometry +0+0 -background none montage.jpg
Run Code Online (Sandbox Code Playgroud)
sda*_*aau 14
我同意-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
Run Code Online (Sandbox Code Playgroud)
# "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
Run Code Online (Sandbox Code Playgroud)
# #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
Run Code Online (Sandbox Code Playgroud)
# #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
Run Code Online (Sandbox Code Playgroud)
# #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
Run Code Online (Sandbox Code Playgroud)
# #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
Run Code Online (Sandbox Code Playgroud)
# #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
Run Code Online (Sandbox Code Playgroud)
# #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
Run Code Online (Sandbox Code Playgroud)
# #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
Run Code Online (Sandbox Code Playgroud)
# #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
Run Code Online (Sandbox Code Playgroud)
# #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
Run Code Online (Sandbox Code Playgroud)
# #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
Run Code Online (Sandbox Code Playgroud)
好吧,希望这对您有用,
干杯!
编辑:我为 ImageMagick,tkGui_ImageMagick.py组合了一个小的 Python/Tkinter/PIL GUI - 最后我可以找到适合我想要的东西的正确命令行:制作四个图像的蒙太奇,其中瓷砖的高度和宽度匹配到该列的最大宽度(或行的高度)。
在这个例子中,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
Run Code Online (Sandbox Code Playgroud)
注意这里,如果我们-extents直接在蒙太奇行中使用,像这样:
montage \
img1.png -extent 300x200 -gravity center \
img2.png -extent 0x400 -gravity North \
-tile 1x -geometry +0+0 \
bmp:-
Run Code Online (Sandbox Code Playgroud)
......我们会发现,对于高度(200)的第一个规范将被忽略,而400时,才会将应用到瓷砖反正!
因此,我们必须控制每个单独的图像的填充(通过调用convert与extents对每个) -然后避免extents在montage线; 因此,我们必须先验地知道每列的(最大)宽度(以及每行的高度)。另请注意:
convert)montage, 通常-gravity必须在(完全指定: w & h) 之后-extent; 在convert,-gravity工作之前 -extent(通常)| 归档时间: |
|
| 查看次数: |
10065 次 |
| 最近记录: |