如何使用网格分割图像并保留透明边界框 [英] How to split an image with a grid and preserve transparency bounding box
问题描述
我有一些png图片,我想将其分成几部分,例如按网格或大小.
I have some png images that I want to split it into parts, like by grid or size.
但是每个部分都应具有与原始图像相同的边界框(透明度).
But each part should have the same bounding box (transparency) as original image.
示例:
将图像分成两部分.
原文:200××89
输出:
part_1.png,200××89
part_1.png, 200 × 89
part_2.png,200××89
part_2.png, 200 × 89
ImageMagick可以这样做吗?或任何其他应用程序或方法.
Can ImageMagick do this? Or any other app or method.
我的实际目标是将图像分割成100多个切片.
My actual goal is to split into 100+ slices images.
另一个目标是每个切片都有一个缩进.说indent = 10px
.
Another goal to have an indents for each slice. Say indent = 10px
.
示例:
输入: 200 x 100
Input: 200 x 100
输出:
part_1.png,200 x 100
part_1.png, 200 x 100
part_2.png,200 x 100
part_2.png, 200 x 100
作为示例,以可视方式比较输入和输出:在Photoshop中合并的输出图像作为一层添加到另一层 200 x 100:
And just as example, to visually compare input and output: combined output images in Photoshop as layer added one onto another 200 x 100 :
这也显示了添加到组合中的输入图像(因此最好查看裁剪的内容和方式):
Also this is showing input image added onto combined(so it's better to see what was cropped and how):
推荐答案
在ImageMagick中,您可以使用-crop命令将图像分为多个部分.对于上面的示例,该示例分为两部分,您可以使用以下命令来完成. ImageMagick将在输出文件名后附加-0,-1....
In ImageMagick, you can split an image into many parts with the -crop command. For your example above with two parts, you can do that with the following commands. ImageMagick will append -0, -1 ... to the output file names.
ImageMagick 6:
ImageMagick 6:
dim=`convert image.png -format "%wx%h" info:`
convert \( -size $dim xc:none \) null: \( image.png -crop 50x100% \) -layers composite result.png
ImageMagick 7:
ImageMagick 7:
magick \( image.png -set option:dim "%wx%h" -crop 50x100% \) null: \( -size "%[dim]" xc:none \) -reverse -layers composite result.png
结果是:
请参见
http://www.imagemagick.org/Usage/crop/#crop一个> http://www.imagemagick.org/Usage/crop/#crop_percent http://www.imagemagick.org/Usage/crop/#crop_tile http://www.imagemagick.org/Usage/crop/#crop_quad http://www.imagemagick.org/Usage/crop/#crop_equal
http://www.imagemagick.org/script/command -line-options.php#layers
请注意,如果您之后不添加+ repage,则-crop保留虚拟画布信息.因此,要将单个图像放回原始位置,必须将它们合成到输入大小的透明背景上.这是在使用-layers组合的命令中使用null:分隔符完成的.
Note that -crop keeps the virtual canvas information if you do not add +repage afterwards. So to put the individual images back into their original placement, you have to composite them onto a transparent background the size of the input. That is done in one command using -layers composite using the null: separator.
这篇关于如何使用网格分割图像并保留透明边界框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!