拆分图像是一个python软件包,您可以从命令行中使用,将图像拆分为图块。
pip install split-image
从命令行:
split-image [-h] [-s] image_path rows cols
作为常规模块导入:
from split_image import split_image
split_image ( image_path , rows , cols , should_square , should_cleanup , [ output_dir ])
# e.g. split_image("bridge.jpg", 2, 2, True, False)
split-image cat.png 2 2
这将cat.png
图像分成4个图块( 2
行和2
列)。
split-image bridge.png 3 4 -s
这将bridge.png
图像分配为12个瓷砖( 3
行和4
列)。 -square
参数在将图像分开之前将图像调整为正方形。用于填充正方形的背景颜色是从图像自动确定的。
split-image cat.jpg 2 2 -r
将尝试合并类似命名的图像图块与一个图像。因此,如果您在当前目录中有这些图像:
cat_0.jpg
cat_1.jpg
cat_2.jpg
cat_3.jpg
他们将根据文件名合并:
split-image image_dir 4 2
将拆分image_dir
文件夹中包含的所有图像。
split-image test.jpg 4 2 --cleanup
过程后将删除原始图像。
split-image test.jpg 4 2 --load-large-images
使用大图像(超过178,956,970像素)时,您可能会遇到错误。传递此标志以覆盖此。
split-image test.jpg 2 2 --output-dir <dir-name>
为图像图块设置输出目录(例如'Outp/images')。默认为当前工作目录。
split-image test.jpg 3 4 --quiet
运行时会抑制所有日志消息(错误和警告除外)。
positional arguments:
image_path The path to the image or directory with images to split.
rows How many rows to split the image into (horizontal split).
cols How many columns to split the image into (vertical split).
optional arguments:
-h, --help Show this help message and exit
-s, --square If the image should be resized into a square before splitting.
-r, --reverse Reverse the splitting process, i.e. merge multiple tiles of an image into one.
--cleanup After splitting or merging, delete the original image/images.
--load-large-images Pass this flag for use with really large images.
--output-dir <dir-name> Set the output directory for image tiles (e.g. 'outp/images').
--quiet Run without printing any messages.
猫照片由Manja Vitoloil在Unsplash上
兰斯·阿斯珀(Lance Asper)的桥梁照片