Extract tiles in a grid from a single PNG?

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
tabicat
Posts: 2
Joined: 2011-10-28T19:09:22-07:00
Authentication code: 8675308

Extract tiles in a grid from a single PNG?

Post by tabicat »

I know questions like this get asked a lot, but I haven't been able to find an answer to my specific use-case.

I frequently have to extract rectangular tile images that are laid out in a grid in a single PNG. The image contains a grid of M x N tiles. The upper-left corner of the upper-left tile in the grid is at offset (X, Y) in pixels. Each tile is WxH pixels in size. In addition, each tile in a row is DX pixels from upper-left corner to upper-left corner (so that DX >= W), and each column is DY pixels below the one above it (DY >= H).

In this picture, M=3 and N=2
Image

Give all these variables, could someone please tell me what command to use to extract the six tiles into their own PNG files? I'm assuming it's using the 'convert' command.

Thanks.
Post Reply