Cropping into divisions with negative initial offsets

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
snake211

Cropping into divisions with negative initial offsets

Post by snake211 » 2010-05-24T11:41:01-07:00

Hi,

First, I have to acknowledge that I have been getting great help from this forum. My deep gratitude to everyone here.

And, here is my problem:
I have a large image to crop into tiles (yes, I want to make custom tile for Google Maps API).

When I omit the x,y offset of the 'crop' parameters, ImageMagick crops the image into "roughly equally sized divisions" ( http://www.imagemagick.org/Usage/crop/#crop_equal ).

What I want is very similar to this. However, instead of getting the 'remainder' tiles at the East and South sides, I want "equally sized remainders" in all 4 sides (North, South, East, and West).

For example, if I have 22x22 image to be cropped into 10x10 tiles, I want the below:
-------------------------------------------------------------------------
tile#1 (1x1) | tile#2 (10x1) | tile#3 (10x1) | tile#4 (1x1)
------------------+-----------------+------------------+----------------
tile#5 (1x10) | tile#6 (10x10)| tile#7 (10x10)| tile#8 (1x10)
------------------+-----------------+------------------+----------------
tile#9 (1x10) |tile#10(10x10)|tile#11 (10x10)| tile#12 (1x10)
------------------+-----------------+------------------+----------------
tile#13 (1x1) | tile#14 (10x1)| tile#15 (10x1) | tile#16 (1x1)
-------------------------------------------------------------------------

I think this will be possible if there is a way to start the cropping with negative x and y offsets. However, once I add those, it changes to a "single" crop instead of "multiple" crops.

Please help me out. Thanks again.

J.R.

User avatar
fmw42
Posts: 23852
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cropping into divisions with negative initial offsets

Post by fmw42 » 2010-05-24T12:40:02-07:00


snake211

Re: Cropping into divisions with negative initial offsets

Post by snake211 » 2010-05-24T12:51:07-07:00

Hi fmw42,

Thanks for the reply.

However, http://www.imagemagick.org/Usage/crop/#crop_neg_offset only crops a single tile. What I want is a bulk cropping (like the one without offset parameters, http://www.imagemagick.org/Usage/crop/#crop_equal). So I am looking for a combination of the two (negative offset + equally sized divisions).

Did I miss something?

J.R.

User avatar
fmw42
Posts: 23852
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cropping into divisions with negative initial offsets

Post by fmw42 » 2010-05-24T15:24:45-07:00

no direct way that I know of currently

User avatar
anthony
Posts: 8871
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Cropping into divisions with negative initial offsets

Post by anthony » 2010-05-24T19:57:10-07:00

First there are two tile cropping methods.

The original and default method is used when no positional offset is given.
http://www.imagemagick.org/Usage/crop/#crop_tile

Code: Select all

convert rose: -crop 23x15  +repage  +adjoin  rose_23x15_%02d.gif
this however may leave small remainder images.

The NEW method only recently added by me (christmas last year), trys to divide the image
into near-equal sized divisions, where you give the number of divisions.

Code: Select all

convert rose: -crop 3x3@  +repage  +adjoin  rose_3x3@_%d.gif
For this you MUST give an '@' sign. and the tiles may vary in size by one pixel.
It is designed so that the sizes are spread evenly across the image, and will be symetricaly centered for an odd number of divisions.

------
You want to use the first normal tile crop, but with remainders spread out around the edge.

The solution, is to remember that normal Tile Cropping divides the image basied on its virtual canvas and not the actual image itself.

Increase the size of the images virtual canvas to a multiple of the size wanted. Make sure the
image is positioned in the center of that virtual canvas. Remember that offset.

Now tile crop.

Now either delete the offsets of the tiles (if not wanted), OR subtract that offset from all the parts using a relative offset repage using a (! flag)

For example..
Subdivide a rose (70x46) into 4 30x20 sized parts with remainders around the edge.
As we want four piece we will need an even number of divisions. That means a virtual canvas size (even multiple of 30x20) of 120x80, making the centering offset X=(120-70)/2 and Y=(80-46)/2 or +25+17 So lets do it!

Code: Select all

convert rose:  -repage 120x80+25+17 -crop 30x20 -repage -25-17\!   rose_30x20_%02d.png
identify -format "%f  %wx%h  %X%Y\n" rose_30x20_*.png
rose_30x20_00.png 5x3 +0+0
rose_30x20_01.png 30x3 +5+0
rose_30x20_02.png 30x3 +35+0
rose_30x20_03.png 5x3 +65+0
rose_30x20_04.png 5x20 +0+3
rose_30x20_05.png 30x20 +5+3
rose_30x20_06.png 30x20 +35+3
rose_30x20_07.png 5x20 +65+3
rose_30x20_08.png 5x20 +0+23
rose_30x20_09.png 30x20 +5+23
rose_30x20_10.png 30x20 +35+23
rose_30x20_11.png 5x20 +65+23
rose_30x20_12.png 5x3 +0+43
rose_30x20_13.png 30x3 +5+43
rose_30x20_14.png 30x3 +35+43
rose_30x20_15.png 5x3 +65+43
We get 16 images. 4 full sized and 12 reminder pieces around the edges.

I'm adding an example of this to IM Examples Tile Cropping.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

User avatar
fmw42
Posts: 23852
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cropping into divisions with negative initial offsets

Post by fmw42 » 2010-05-24T20:55:33-07:00

I tried something similar but ignorantly use -page instead of -repage. I was going to get back to -repage after I realized that -page was wrong, but did not get time before Anthony answered in full detail. But his solution is more complex than I had anticipated.

User avatar
anthony
Posts: 8871
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Cropping into divisions with negative initial offsets

Post by anthony » 2010-05-24T21:51:06-07:00

Not that complex. The key is the offset needed.

The only reason I used a 120x80 virtual canvas as a starting point
rather than a smaller 90x60 canvas was that I wanted 4 full 30x20 images.

To get the maximum number of full images (centered) from the original you would divide the original image by the tile size. That gives 70x46 / 30x20 => 2x2 + remainer.

To hold the remaider you want 2 tiles on either side, so you get 4x4 tiles.
Multiply that by the tile size and you get 30x20 * 4x4 => 120x80 canvas size.
Subtract the original image and divide by 2 to get center offset ( 120x80 - 70x46 ) / 2 => +25+17

So the virtual canvas and offset needed is 120x90+25+17


NOTE: it is possible that the remainder will not divide equally into 2 so you could have one remainder size one pixel smaller (or larger) than the other.

This has been added to IM Examples, Cutting and Bordering, Center Tile Cropping
http://www.imagemagick.org/Usage/crop/# ... e_centered

Fred I gather you plan to create a script for this!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

User avatar
anthony
Posts: 8871
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Cropping into divisions with negative initial offsets

Post by anthony » 2010-05-24T22:22:09-07:00

Additional.

If you did not add two columns and rows to the number of tiles and thus generate a negative offset you can ignore the remainders.

Code: Select all

  convert rose: -repage 60x40-5-3 -crop 30x20 +repage rose_ctiles_%d.gif
Basically as the remainders are now 'outside' the calculated virtual canvas they are ignored by the tile crop operation.

Now that is tricky!!!! 8)

That has also been added to the IM examples
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

matteosistisette
Posts: 17
Joined: 2011-10-10T09:04:08-07:00
Authentication code: 8675308

Re: Cropping into divisions with negative initial offsets

Post by matteosistisette » 2011-10-10T09:08:02-07:00

Hi,

Hey anthony, is there a way to use the "new" equally-sized MxN@ crop AND a repage together?

Basically, if I do -repage WxH -crop MxN@, the repaging is ignored!!

User avatar
anthony
Posts: 8871
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Cropping into divisions with negative initial offsets

Post by anthony » 2011-10-10T16:34:16-07:00

The equal area crop does not use the virtual canvas, other than as an adjustment for the offset to each tile for the actual cropping process for each tile that it generates. Basically I never considered to have it work with regards to the virtual canvas, though I suppose it could be modified to do so. For most images the canvas is the same as the real image.

It does preserve the canvas offsets for each tile it crops, so you have a record of where that tile came from with regard to the canvas and the original image (just as all crops do). To remove those add a +repage after the crop.

Best idea is to do the calculations you need yourself to do the initial crop into larger sections (separating the reminder from the rest), and then use the normal tile or near-equal-area crop to sub-divide those images appropriately.

I can't really tell you more without an actual example. For example image input dimensions, and desired results.

Really it all comes down to maths. I added near-equal-area crop as the maths for that can be tricky, especially as I wanted the 'odd sized' tiles to be evenly distributed as possible, and to handle overlaps, skips, and two types of edge handling.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

Post Reply