title: Automatically find frame sizes from png sprite sheet
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/14432021/pyside-separating-a-spritesheet-separating-an-image-into-contiguous-regions-o">PySide: Separating a spritesheet / Separating an image into contiguous regions of color</a>  </p>
  </blockquote>
  
  
  
  <p>Given a .png image with transparent pixels and a grid of individual animation frames (where the last row need not be full), how would you automatically find the dimensions of each individual frame, and detect how many frames are in the .png?</p>
  
  <p>I am trying to convert the resources from the creative-commons treasure-trove of <a href="http://glitch.com" rel="nofollow noreferrer">Glitch</a> into our internal format, and I am having problems isolating frame information from the raw .pngs.</p>
  
  <p><img src="https://i.stack.imgur.com/ttXm6.png" alt="sample .png, from glitch.com">
  (released by Glitch under <a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US" rel="nofollow noreferrer">http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US</a> license)</p>
  
  <p>In this case, I can find that frames are 189 x 230 px; but looking this up requires time, and there are a lot of images to potentially look up.</p>
  
  <p>I would like to split the image into frames for use inside a Java Swing desktop application. I can load the image using <code>ImageIO</code> into a <code>BufferedImage</code>, and easily check for pixel transparency. There are only a few possible frame sizes: given a 945x690 frame as in the example, and assuming a minimal side of 50px, the only plausible frame-widths are 5 x 189 (correct), 7 x 135, or 9 x 105.</p>
  
  <p>So, how would you find the frame size? This does not need to be tremendously efficient, as resource conversion is a one-time problem. Pseudo-code answers are fine; I am mostly interested in the algorithm.</p>
  
  <p><strong>Note:</strong> <a href="https://stackoverflow.com/questions/14432021/pyside-separating-a-spritesheet-separating-an-image-into-contiguous-regions-o">PySide: Separating a spritesheet / Separating an image into contiguous regions of color</a> explains how to deal with non-animation sprite-sheets, with irregularly sized images within the sheet. I am interested in detecting rows x columns, which can be solved with a much simpler algorithm (see accepted answer).</p>