5

I have a large image made up of lots of smaller images (not-touching) on a transparent background. Like a spritesheet, but the sprites aren't all the same size, nor are they laid out on a grid.

Can I use ImageMagick to split the image into smaller images?

So, for example, this: (where # = colored pixel)

  #   ##
  #   # 
    # # 
  ###   

Becomes these

#
#

##
#
#

  #
###
bluepnume
  • 16,460
  • 8
  • 38
  • 48
  • I don't know how to do this in ImageMagick, but look up texture atlases. The complexity of your question depends a great deal on whether you want IM to detect the congruent areas, or if you have this information at your disposal already (predefined masks, that is). – appas Nov 26 '11 at 02:11

3 Answers3

4

You can do this in one single command and do it very efficiently. For example, this command:

time convert \
   very-very-large.png \
  -quality 85 \
  -write mpr:mpc:label \
  +delete \
     mpr:mpc:label -crop '3000x2001+0+491' -resize '170x116!>'   -write pic1.png +delete \
     mpr:mpc:label -crop '2981x2883+8+0'   -resize '75x75!>'     -write pic2.png +delete \
     mpr:mpc:label -crop '1100x1983+0+0'   -resize '160x160!>'   -write pic3.png +delete \
     mpr:mpc:label -crop '2000x2883+0+0'   -resize '1024x960!>'  -write pic4.png +delete \
     mpr:mpc:label -crop '1000x2883+0+0'   -resize '190x188!>'   -write pic5.png +delete \
     mpr:mpc:label -crop '3000x2000+0+0'   -resize '2048x2047!>' -write pic6.png +delete \
     mpr:mpc:label -crop '3000x2883+0+0'   -resize '595x421!>'   -write pic7.png +delete \
     mpr:mpc:label -crop '3000x2883+0+0'   -resize '3000x2883!>' -write pic8.png

will cut and save (under different names) 8 different sub-images from the large one.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
0

If you do not want to look up the coordinates for segmenting your image automatically, you can try the segment_image script from the ImageMagick example scripts area. It uses ImageMagick commands to do simple auto-segmentation.

A bit of background and usage instructions for this script its available here.

tanius
  • 14,003
  • 3
  • 51
  • 63
0

Use three crop commands, one for each section:

convert mosaic -crop 1x2+0+0 part1
convert mosaic -crop 2x3+2+0 part2
convert mosaic -crop 3x2+0+2 part3
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265