Page 1 of 1

Rolling crop from long image

Posted: 2019-04-03T12:16:09-07:00
by tomchambers
My image is 30000x256px. I want to create 30k 256x256px images using a crop every 1px horizontally.

So first crop is 256x256+0+0
Second crop is 256x256+0+1
...

Code: Select all

convert original.png -crop 256x256  segments/segment.png
This gets me part of the way there in that it gives me a crop every 256px. But I want the crops to be overlapping. Can I do this?

Re: Rolling crop from long image

Posted: 2019-04-03T12:45:33-07:00
by fmw42
I do not understand. You want to shift in X not Y by 1 pixel. You have said
Second crop is 256x256+0+1
. Should that not be Second crop is 256x256+1+0?

To do that you will have to write a script loop and read your input 30k times. I do not see a way to do that with one read of the input image. ImageMagick does not yet have an offset increment -define as far as I know.

You can reduce the number of reads by 256 (3000/256), by doing the 256x256 crop. Then roll the image by 1 pixel horizontally and do it again. Etc.

Re: Rolling crop from long image

Posted: 2019-04-03T13:26:18-07:00
by tomchambers
Sorry, yes that's correct, shifting by X not Y so +1+0.

Ok, I guess I'll have to do it with a script. I'm not quite sure what you mean by the second part. Could you give an example command? Surely if I do the 256x256 crop and then roll it I will go outside of the newly cropped area?

Re: Rolling crop from long image

Posted: 2019-04-03T13:26:45-07:00
by snibgo
"@" can give overlapping crops. For example:

Code: Select all

magick -size 2000x256 xc: -crop 2000x1+255@ info:

xc:[0] XC 256x256 2000x256+0+0 16-bit sRGB 7.766u 0:02.312
xc:[1] XC 256x256 2000x256+1+0 16-bit sRGB 7.766u 0:02.312
xc:[2] XC 256x256 2000x256+2+0 16-bit sRGB 7.813u 0:02.327
:
:
xc:[1742] XC 256x256 2000x256+1742+0 16-bit sRGB 4.328u 2:02.611
xc:[1743] XC 256x256 2000x256+1743+0 16-bit sRGB 4.328u 2:02.642
xc:[1744] XC 256x256 2000x256+1744+0 16-bit sRGB 4.328u 2:02.657
Note that you will need about 15 GB of memory.

Re: Rolling crop from long image

Posted: 2019-04-03T15:10:33-07:00
by fmw42
Hey, snibgo. I did not know you could add offsets to the equal size crops to do that. Neat! Anthony should add that to his docs at https://imagemagick.org/Usage/crop/#crop_equal. I have sent him a message to see if he will do that.

Re: Rolling crop from long image

Posted: 2019-04-03T15:46:54-07:00
by snibgo
It is documented there:
As a bonus, you can also sub-divide the image so that each tile will 'overlap' its neighbours. You do this by not only using a '@' flag but also specifying the number of pixels of overlap you want.
...

Re: Rolling crop from long image

Posted: 2019-04-03T16:20:12-07:00
by fmw42
Thanks. I had overlooked that.

Re: Rolling crop from long image

Posted: 2019-04-05T02:10:47-07:00
by tomchambers
Thanks both, very helpful!

If I wanted to adjust that so that I only have say a 32px overlap between crops, how would I manage it?

I tried

Code: Select all

magick -size 2000x256 xc: -crop 2000x224+32@ info:
but this results in a command that takes a very long time to run.

Re: Rolling crop from long image

Posted: 2019-04-05T05:35:55-07:00
by snibgo
I suggested:

Code: Select all

-crop 2000x1+255@
The numbers are:

2000 to chop into up to 2000 images horizontally.
1 to chop into up to 1 image vertically (ie no vertical chopping).
255 to give a 255 pixel horizontal overlap.

Your command has asked to chop into 224 images vertically, in addition to 2000 horizontally, resulting in 2000*224 = 448,000 images. That's why it takes a long time, and I don't think you want that.