New available syntax for command `split` .

After several times needing a command to split an image into two pieces, one of which having a fixed size (e.g. 32 px), I decided to enhance the split command with a new native syntax.

Previously, it was possible to split an image into fixed-size blocks along an axis, for instance, for 32px blocks:

$ gmic sample colorful,128 split x,-32

But in this case, if the image width is larger than 64, then you end up with more than 2 blocks. In the example above, you actually get 4 images:

And if you want only to split your images in two parts, then you have to re-append the last images together in a second step, like:

$ gmic sample colorful,128 split x,-32 a[^0] x

But honestly, that’s a bit a shame to have to “fix” what the previous command did too much.

Another solution would be to use the crop command:

$ gmic sample colorful,128 +crop 32,100% crop.. 0,31

But it’s a bit tedious to write.

Now, in G’MIC 3.6.0_pre, you can force the command split to split an image while specifying the max number of parts you want, as an additional argument (here, max_parts=2 );

$ gmic sample colorful,128 split x,-32,2

Another useful addition is that you can also specify the size of the split as a percentage of the image dimension, like:

$ gmic sample colorful,128 split x,-33.333%,2

The example above will split the image in 2 parts : one representing 1/3 of the image width, the other, 2/3:

There you go, nothing too amazing, but I’m sure it will come in handy some day :slight_smile:

4 Likes

Looks like I will have to rewrite the rep_multisplit I did. Shouldn’t be hard that you address the shortcoming with the splits.

Your edits made it more sensible. Was planning to say something, but decided to let it cook. Curious what were you working on that required split. I don’t remember using splits, except for quick one-liners. Maybe image or crop.

Not sure if you’re talking in general or not? And split is used all the time, just that in some case, extension of it is used and I think that is what you’re referring to. In my case, there’s a param in rep_zip which splits according to sections, and that’s where I made my own version of split.

Not needed every day, but whenever you need it these changes being available are going to be very nice indeed.