title: How to divide an image into ''n'' different polygons in Python
body: <p>I am new to python and am trying to divide an image into 'n' different polygon using python. My target is to convert an image into n random polygon shaped images. I tried Voronoi algorithm but its kind of messy. I would really appreciate any help. Any other segmentation method etc.</p>
  <p>My previous Code:</p>
  <pre><code>import random
  import matplotlib.pyplot as plt
  import numpy as np
  from scipy.spatial import Voronoi, voronoi_plot_2d
  
  img = plt.imread(&quot;abc.jpg&quot;)
  fig, ax = plt.subplots()
  ax.imshow(img)
  
  def points(radius,rangeX,rangeY,qty):
      deltas = set()
      for x in range(-radius, radius+1):
          for y in range(-radius, radius+1):
              if x*x + y*y &lt;= radius*radius:
                  deltas.add((x,y))
  
      randPoints = []
      excluded = set()
      i = 0
      while i&lt;qty:
          x = random.randrange(*rangeX)
          y = random.randrange(*rangeY)
          if (x,y) in excluded: continue
          randPoints.append((x,y))
          i += 1
          excluded.update((x+dx, y+dy) for (dx,dy) in deltas)
      return randPoints
  
  
  
  def plot1(randPoints,fig):
      points = np.array(randPoints)
      vor = Voronoi(points)
      print vor.vertices
      voronoi_plot_2d(vor,ax = fig.gca())
      #plt.savefig('abc.png')
      plt.show()
      
  radius = 20
  rangeX = (0, 960)
  rangeY = (0, 480)
  qty = 9
  
  points = points(radius, rangeX, rangeY, qty)
  plot1(points,fig)
  </code></pre>
  <p>My Input:
  <a href="https://i.stack.imgur.com/fDHBM.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fDHBM.jpg" alt="enter image description here" /></a></p>
  <p>My output:
  <a href="https://i.stack.imgur.com/T0WaG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/T0WaG.png" alt="enter image description here" /></a></p>
  <p>This is for n = 9 I would appreciate any help I can get.</p>