文件名中带有 row/column 的 ImageMagick 裁剪仅保存最后一张图像

ImageMagick crop with row/column in file name only saving last image

我正在尝试使用 ImageMagick 和 PowerShell 裁剪图像。我可以使用以下命令很好地裁剪图像,它创建了 2000 多张图像:

convert -crop 16x16 .\original.png tileOut%d.png

但是,我想利用 ImageMagick 动态设置文件名的能力。

根据 a post on their forums,我应该可以通过批处理文件 运行 像下面这样的东西:

convert ^
   bigimage.jpg ^
  -crop 256x256 ^
  -set filename:tile "%%[fx:page.x/256+1]_%%[fx:page.y/256+1]" ^
  +repage +adjoin ^
  tiled_%%[filename:tile].gif

我不需要转义 % 因为我是直接在 PowerShell 中 运行 宁这个,所以我使用了以下内容:

convert -crop 16x16 .\original.png -set filename:tile "%[fx:page.x/16+1]_%[fx:page.y/16+1]" +repage +adjoin directory\tiled_%[filename:tile].png

然而,当我 运行 这个命令时,我最终得到一个名为 tiled_%[filename 的文件和另一个名为 tiled_45_47.png 的文件.

所以虽然它看起来确实创建了最后一个文件,但它只创建了一个。根据文件的属性,第一个文件的大小为 0 字节,但在磁盘上占用了超过 8 MB space。

尝试 运行 批处理文件中的命令会导致相同的行为,这让我认为 PowerShell 本身不是问题,而是命令。

根据文档 +adjoin is required since I want different images. +repage doesn't make much sense to me, but I've kept it in the command since the original had it, and excluding it doesn't seem to change the output. -set filename 看起来很简单。

第一个的大尺寸让我相信之前的所有图像都可能被添加到其中。然而,文件名也表明它挂在 : 上,但它似乎不是 PowerShell 中的特殊字符。它还为最后一次裁剪创建图像。莫名其妙。

那我做错了什么?

提前致谢!

编辑:

根据评论,我认为问题可能出在 ImageMagick 命令上。

我没有使用 Powershell,但我认为通过先指定图像,然后裁剪,然后设置文件名,您会取得更大的成功:

convert original.png -crop 16x16 -set filename:tile "%[fx:page.x/16+1]_%[fx:page.y/16+1]" +repage "tiled_%[filename:tile].png"

所以以前我是用下面的命令来裁剪图片的,%d会根据顺序自动转换成数字。

convert -crop 16x16 .\original.png directory\tileOut%d.png

效果很好。但是,该论坛上提供的示例将原始文件名列为转换命令的第一个参数。更改我的命令以使其首先列出会导致预期的行为。

convert .\original.png -crop '16x16' -set 'filename:tile' '%[fx:page.x/16+1]_%[fx:page.y/16+1]' +repage +adjoin 'directory\tiled_%[filename:tile].png'

可能不需要在这么多地方使用单引号,但它确实有效。