如何编写一个使用 image magick 将图像切割成碎片的 bash 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1491547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:14:12  来源:igfitidea点击:

How to write a bash script that cuts images into pieces using image magick?

linuxbashimagemagick

提问by Thomi

I have a number of input images that contain multiple smaller images, all of them in a single row. All the contained images are the same size. So, for example, the image input.pngmay be 480x48and contain 10 48x48images, all in one row.

我有许多包含多个较小图像的输入图像,它们都在一行中。所有包含的图像都是相同的大小。因此,例如,图像input.png可能480x48包含 10 个48x48图像,全部在一行中。

Using the imagemagick converttool (or any other tool supplied with the defaul imagemagick suite), I want to write a bash script that takes an input image, the number of images to cut, and then cuts them all into individual images.

使用 imagemagickconvert工具(或默认 imagemagick 套件提供的任何其他工具),我想编写一个 bash 脚本,该脚本获取输入图像、要剪切的图像数量,然后将它们全部剪切成单个图像。

The user interaction i can do, but I've not been able to get convertto do the actual cutting. Can anyone suggest something? From reading the manual pages, i think this should work:

我可以进行用户交互,但我无法进行convert实际切割。任何人都可以提出建议吗?通过阅读手册页,我认为这应该有效:

convert 'input.png[0x0+48+48]' output.png

convert 'input.png[0x0+48+48]' output.png

but I get an error:

但我收到一个错误:

convert: no pixels defined in cache tb_icons_l.png' @ magick/cache.c/OpenCache/3572. convert: No IDATs written into file 1.png' @ coders/png.c/PNGErrorHandler/1391.

转换:缓存tb_icons_l.png' @ magick/cache.c/OpenCache/3572. convert: No IDATs written into file 1.png 中没有定义像素 '@coders/png.c/PNGErrorHandler/1391。

Any ideas?

有任何想法吗?

回答by psychoschlumpf

I would do it like that:

我会这样做:

convert input.png -crop 48x48  +repage  +adjoin  output_%02d.gif

Read more at Tile Cropping, in ImageMagick documentation.

在 ImageMagick 文档中的Tile Cropping 中阅读更多内容

回答by Paused until further notice.

I believe you've got the position and size swapped. Try:

我相信你已经交换了位置和大小。尝试:

convert 'input.png[48x48+0+0]' output.png

The third image would be:

第三张图是:

convert 'input.png[48x48+96+0]' output.png

Or

或者

convert 'input.png[48x48+0+96]' output.png