title: Separate/reposition/translate shapes in image with pillow in python
body: <p>I need to separate or translate or replace pixels in an image with python so as to all the shapes to share the same distance between each other and the limits of the canvas.</p>
  
  <p>background is white, shapes are black.</p>
  
  <p>IMPORTANT: Images are dynamic, all images have bars at different positions, that means i need to detect where a bar starts and where a bar ends to draw the final image!</p>
  
  <p>This is an example INPUT image</p>
  
  <p><a href="https://i.stack.imgur.com/St6PV.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/St6PV.jpg" alt="enter image description here"></a></p>
  
  <p>This is an example OUTPUT image</p>
  
  <p><a href="https://i.stack.imgur.com/JGO0w.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JGO0w.jpg" alt="enter image description here"></a></p>
  
  <p>I did this by hand, i don't know if all bars are separated by the same distance, it's just an example.</p>
  
  <p>The resulting canvas size can change too but ideally i would like all canvas to have the same width and height after the bars have been respositioned.</p>
  
  <p>All pixels in the images are BLACK or WHITE, that should make it much easier.</p>
  
  <p>What i've tried so far kind of detects the shapes but some shapes are not detected correctly when they are too close of each other:</p>
  
  <pre><code>saut = 1
  start_ab = 0
  end_ab = 0
  start_or = im2.size[1] - 1
  end_or = 0
  size = 0
  
  shapes = []
  
  for y in range(0, im2.size[0], saut):  # slice across
      for x in range(0, im2.size[1], 1):  # slice down
          pix = im2.getpixel((y, x))
          if pix != 255:
              start_or = min(start_or, x)
              end_or = max(end_or, x)
              inshape = True
  
      if foundshape == False and inshape == True:
          foundshape = True
          start_ab = y
  
      if foundshape == True and inshape == False:
          foundshape = False
          end_ab = y
          size = max(size, end_ab)
          shapes.append((start_ab, end_ab, start_or, end_or))
          start_or = im2.size[1] - 1
          end_or = 0
      inshape = False
  
  print shapes
  # example shapes output NOT FROM THE EXAMPLE IMAGES PROVIDED but from other shapes [(54, 171, 72, 233), (216, 324, 108, 251), (342, 486, 0, 215), (513, 765, 18, 260), (792, 918, 90, 242)]
  </code></pre>
  
  <p>And still have to draw the new image, i don't know how to do that, also some images have shapes with "holes" in them, so that makes the "redraw" a little bit more complicated.</p>