裁剪瓷砖的结果?

huangapple go评论117阅读模式
英文:

Crop the result of a tile?

问题

I'm dumping images from an old game, and for whatever reason they come divided in chunks of 64x64. I tile them to make up the 640x512 picture, fix the alpha, and then I need to crop off some dummy pixel rows from the bottom so that it is 640x480.

Thing is, I can't seem to make the crop work correctly. How do I make the crop apply over the result of this, and have it all be in a single command?

magick montage -tile 10x8 -geometry +0+0 -alpha off *.png out.png

I'm on 7.1.0 on W11.

英文:

I'm dumping images from an old game, and for whatever reason they come divided in chunks of 64x64. I tile them to make up the 640x512 picture, fix the alpha, and then I need to crop off some dummy pixel rows from the bottom so that it is 640x480.

Thing is, I can't seem to make the crop work correctly. How do I make the crop apply over the result of this, and have it all be in a single command?

magick montage -tile 10x8 -geometry +0+0 -alpha off *.png out.png

I'm on 7.1.0 on W11.

答案1

得分: 1

以下是翻译好的部分:

让我们为插图创建 4 个圆形渐变:

magick -size 100x100 radial-gradient:magenta-black -duplicate 3 %d.png

这里是它们:

-rw-r--r--     1 mark  staff     20307 12 Mar 08:28 0.png
-rw-r--r--     1 mark  staff     20307 12 Mar 08:28 1.png
-rw-r--r--     1 mark  staff     20307 12 Mar 08:28 2.png
-rw-r--r--     1 mark  staff     20307 12 Mar 08:28 3.png

它们都看起来一样:

裁剪瓷砖的结果?

如果你执行以下命令:

magick montage -geometry +0+0 ?.png result.png

你会得到:

裁剪瓷砖的结果?

如果你执行以下命令:

magick montage -geometry +0+0 ?.png -crop 50x50+0+0 result.png

你会得到以下结果,你可以看到它在合并之前进行了裁剪:

裁剪瓷砖的结果?

但如果你将图像合并在一起并将结果发送到 MIFF("Magick Image File Format")格式,然后在另一个 magick 实例中进行裁剪:

magick montage -geometry +0+0 ?.png MIFF:- | magick MIFF:- -crop 200x125+0+0 result.png

你会得到:

裁剪瓷砖的结果?

请注意,你可以在这种情况下将 MIFF:- 替换为 PNG:-,如果你不理解为什么需要它 - 这在使用 64 位 TIFF 时会有用,其中一个中间的 16 位 PNG 无法在两个阶段之间保留所有信息。

我希望你要求一个单一的命令,因为你不想写入磁盘,尽管你没有说明原因,如果原因不同,请解释,我将提供不同的解决方案。

如果你真的只想出于某种原因使用单个进程,你可以使用一个类似 "magick 脚本" 的方式。

首先,将你想要的所有命令放入一个脚本文件中,命名为 croppedMontage.mgk

( 0.png 1.png +append )     # append 0.png and 1.png across page to make a row
( 2.png 3.png +append )     # append 2.png and 3.png across page to make a row
-append                     # vertically stack all previously created rows down the page
-crop 200x125+0+0           # crop result of stacking all rows
-write result.png

然后使用以下命令调用它:

magick -script croppedMontage.mgk

裁剪瓷砖的结果?

请注意,你也可以在命令行上执行上述操作而不需要脚本,但我没有这样做,因为你有一些文件的数量有点繁杂,而且引号使用起来很丑陋。尽管如此,它将如下所示:

magick \
   \( 0.png 1.png +append \) \
   \( 2.png 3.png +append \) \
   -append -crop 200x125+0+0 result.png

请注意,在 Windows 中,引号使用不同,你需要这样做:

magick ^
   ( 0.png 1.png +append ) ^
   ( 2.png 3.png +append ) ^
   -append -crop 200x125+0+0 result.png

因此,这也许是更喜欢脚本方法的另一个原因。

英文:

Let's make 4 circular gradients for illustration:

magick -size 100x100 radial-gradient:magenta-black -duplicate 3 %d.png

Here they are:

-rw-r--r--     1 mark  staff     20307 12 Mar 08:28 0.png
-rw-r--r--     1 mark  staff     20307 12 Mar 08:28 1.png
-rw-r--r--     1 mark  staff     20307 12 Mar 08:28 2.png
-rw-r--r--     1 mark  staff     20307 12 Mar 08:28 3.png

And they all look the same:

裁剪瓷砖的结果?


If you do this:

magick montage -geometry +0+0 ?.png result.png 

You get:

裁剪瓷砖的结果?


If you do this:

magick montage -geometry +0+0 ?.png -crop 50x50+0+0 result.png

You get the following and you can see it did the cropping prior to montaging:

裁剪瓷砖的结果?


But if you montage the images together and send the result in a MIFF "Magick Image File Format" (which can contain any number of layers and any number of bit-depths) to a further instance of magick and crop it there:

magick montage -geometry +0+0 ?.png MIFF:- | magick MIFF:- -crop 200x125+0+0 result.png

You get:

裁剪瓷砖的结果?

Note that you can, in this instance, replace MIFF:- with PNG:- if you don't understand the need for it - it would come into its own with a 64-bit TIFF for example, where an intermediate 16-bit PNG would be unable to retain all information between the two stages.


I'm hoping you asked for a single command because you don't want to write to disk, although you didn't say why, and if so, this solution achieves that. If the reason was different, please explain and I will provide a different solution.


If you really, really only want to use a single process for some reason, you can use a "magick script" like this.

First put all the commands you want in a script file, and name it say croppedMontage.mgk:

( 0.png 1.png +append )     # append 0.png and 1.png across page to make a row
( 2.png 3.png +append )     # append 2.png and 3.png across page to make a row
-append                     # vertically stack all previously created rows down the page
-crop 200x125+0+0           # crop result of stacking all rows
-write result.png

Then invoke it with:

magick -script croppedMontage.mgk

裁剪瓷砖的结果?

Note that you could do the above on the command-line without a script, but I didn't do that because the number of files you have is a bit unwieldy and the quoting is ugly. It would look like this though:

magick \
   \( 0.png 1.png +append \) \
   \( 2.png 3.png +append \) \
   -append -crop 200x125+0+0 result.png

Note that quoting is different in Windows where you want:

magick ^
   ( 0.png 1.png +append ) ^
   ( 2.png 3.png +append ) ^
   -append -crop 200x125+0+0 result.png

So that's maybe yet another reason to prefer the script approach.

huangapple
  • 本文由 发表于 2023年3月12日 13:35:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定