Splitting image with imagemagick

旧巷老猫 提交于 2019-12-18 18:05:44

问题


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

#
#

##
#
#

  #
###

回答1:


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.




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/8273043/splitting-image-with-imagemagick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!