bluepnume
bluepnume

Reputation: 17108

Splitting image with imagemagick

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

#
#

##
#
#

  #
###

Upvotes: 6

Views: 7716

Answers (3)

tanius
tanius

Reputation: 16749

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.

Upvotes: 1

Kurt Pfeifle
Kurt Pfeifle

Reputation: 90193

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.

Upvotes: 4

Paul Sweatte
Paul Sweatte

Reputation: 24617

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

Upvotes: 0

Related Questions