Cropping an image with 16, 32, and 48 pixel rows [SOLVED!]

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
strivinglife
Posts: 3
Joined: 2016-01-09T21:05:37-07:00
Authentication code: 1151

Cropping an image with 16, 32, and 48 pixel rows [SOLVED!]

Post by strivinglife »

Greetings.

I'm still very much a beginner with ImageMagick, but have been trying to use it instead of manually cropping images.

I'm using Windows, and running the commands in PowerShell. Version 6.9.2-8 Q16.

I've been using the following command to crop some regular-sized images.

Code: Select all

convert .\original.png -crop 32x32 -set filename:tile '%[fx:page.y/32+1]_%[fx:page.x/32+1]' +repage +adjoin 'original\tiled_%[filename:tile].png'
This gives me a nice collection of 32x32 tiles with the file name containing the row and column from the original image.

However, I now want to have a bit more control over where my crop starts. With the image I'm currently working with the rows are either 16 pixels, 32 pixels, or 48 pixels tall, but all 32 pixels wide.

Can I have IM slice an image into pieces after defining a starting point?

What I'd like to do, for example, is skip the first row of 16x32 tiles, and then crop the next 7 rows of 32x32 tiles. Then I have another row of 16x32 tiles to skip, followed by 4 rows of 32x32 tiles that I want to grab again. Etcetera.

It looks like I can add to the geometry parameter, which gives me something like the following:

Code: Select all

convert .\original.png -crop 32x32+0+16 -set filename:tile '%[fx:page.y/32+1]_%[fx:page.x/32+1]' +repage +adjoin 'original\tiled_%[filename:tile].png'
Unfortunately, unlike the first command I only get the first 32x32 tile, instead of it cropping the image into 32x32 tiles (starting at 0,16).

I could probably cut out the standardized rows. So in my case, create a new image skipping the first row of 16, grabbing the 7 rows of 32, then create a second image skipping the row of 16, 7 rows of 32, and second row of 16, grabbing the next 4 rows of 32, etcetera. Something like this for the first section of 32x32 tiles:

Code: Select all

convert .\original.png -crop 768x224+0+16 -set filename:tile '%[fx:page.y/32+1]_%[fx:page.x/32+1]' +repage +adjoin 'original\tiled_%[filename:tile].png'
Then this for the second section of 32x32 tiles:

Code: Select all

convert .\original.png -crop 768x224+0+128 -set filename:tile '%[fx:page.y/32+1]_%[fx:page.x/32+1]' +repage +adjoin 'original\tiled_%[filename:tile].png'
And then I run my usual command against the new images I've created.

However, I'm wondering if there's perhaps an easier way to do this? Can I have IM slice an image into pieces after defining a starting point?

Thanks!
Last edited by strivinglife on 2016-01-10T15:05:07-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Cropping an image with 16, 32, and 48 pixel rows

Post by snibgo »

Why not crop twice in the same command? The first (with offsets) to remove the rows and/or columns you don't want; the second (without offsets) to split it into tiles.
snibgo's IM pages: im.snibgo.com
strivinglife
Posts: 3
Joined: 2016-01-09T21:05:37-07:00
Authentication code: 1151

Re: Cropping an image with 16, 32, and 48 pixel rows

Post by strivinglife »

snibgo wrote:Why not crop twice in the same command? The first (with offsets) to remove the rows and/or columns you don't want; the second (without offsets) to split it into tiles.
If that's possible, that sounds great to me!

Do you by any chance have a link that would give me a good way to start down this path?

Thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Cropping an image with 16, 32, and 48 pixel rows

Post by snibgo »

I'm not sure that I understand exactly what you want, but suppose you want to ignore the leftmost 20 columns and top 40 rows, and want to split the rest of the image into 32x32 squares.

Code: Select all

convert in.png -crop 0x0+20+40 +repage -crop 32x32 +repage out_%06d.png
"0x0" in the first crop means use the full with and height.

Instead of the first "-crop", you could use "-chop". See http://imagemagick.org/script/command-l ... s.php#chop
snibgo's IM pages: im.snibgo.com
strivinglife
Posts: 3
Joined: 2016-01-09T21:05:37-07:00
Authentication code: 1151

Re: Cropping an image with 16, 32, and 48 pixel rows

Post by strivinglife »

You Rock.

So that indeed helped me. But thanks to that nudge and the reference to chop I ended up getting exactly what I needed.

Basically I wanted to crop an image after I had removed some rows. This is the command that gets me the image I want to crop from:

Code: Select all

convert .\in.png
-chop 0x16+0+864
-chop 0x16+0+720
-chop 0x16+0+608
-chop 0x16+0+496
-chop 0x16+0+384
-chop 0x16+0+240
-chop 0x16+0+0
'out.png'
Which resulted in the command that I ran which did everything for me:

Code: Select all

convert .\in.png -chop 0x16+0+864 -chop 0x16+0+720 -chop 0x16+0+608 -chop 0x16+0+496 -chop 0x16+0+384 -chop 0x16+0+240 -chop 0x16+0+0 -crop 32x32 -set filename:tile '%[fx:page.y/32+1]_%[fx:page.x/32+1]' +repage 'out\tiled_%[filename:tile].png'
And now I think I (semi-)understand both chop and how the image geometry parameter works!

Thank you, thank you, thank you!

And since it's once again left me grinning, ImageMagick is awesome!
Post Reply