Rolling crop from long image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
tomchambers
Posts: 5
Joined: 2019-03-31T11:26:29-07:00
Authentication code: 1152

Rolling crop from long image

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Rolling crop from long image

Post 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.
tomchambers
Posts: 5
Joined: 2019-03-31T11:26:29-07:00
Authentication code: 1152

Re: Rolling crop from long image

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rolling crop from long image

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Rolling crop from long image

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rolling crop from long image

Post 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.
...
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Rolling crop from long image

Post by fmw42 »

Thanks. I had overlooked that.
tomchambers
Posts: 5
Joined: 2019-03-31T11:26:29-07:00
Authentication code: 1152

Re: Rolling crop from long image

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rolling crop from long image

Post 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.
snibgo's IM pages: im.snibgo.com
Post Reply