title: Creating a filled numpy array of ones from a polygon points Python
body: <p>I would be very gratefull if anyone could help me find a faster solution to my problem.</p>
  
  <p>Heres the scenario:- I have a polygon of float points which I want to map to a grid. The grid cells can be different width and height not uniformed like my image shows. ie rectangular.</p>
  
  <p>I have tried using Image draw but it only uses ints. Converting floats to ints means I have to scale the floats up and remove decimal to keep some precision but image draw will not work with the larger polygon of points. </p>
  
  <p>Is there a more eloquent and fast way of achieving a numpy array of ones (blue) for the filled area of the polygon and zeros (red) for the rest.  I have read a little on mesh grid but cant see how it can be of use for this scenario.</p>
  
  <p>Many thanks</p>
  
  <p>The results from the code are</p>
  
  <pre><code>cols = 4
  rows = 4
  points = [[1535116L, 1725047L], [1535116L, 2125046L], [-464884L, 2125046L], [-464884L, 125046L]]    
  
  bbCut = getPythonBoundBox(points)
  cutWidth =  bbCut[1][0]-bbCut[0][0]
  scale = float(cutWidth) / float(rows)
  ###Center data to origin
  for p in range(len(points)):
      points[p][0] -= (bbCut[1][0] - bbCut[0][0])/2
      points[p][1] -= (bbCut[1][1] - bbCut[0][1])/2
      points[p][0] /= scale
      points[p][1] /= scale
  
  
  ##move points to Zero
  bbCut = getPythonBoundBox(points)
  
  for p in range(len(points)):
      points[p][0] -=bbCut[0][0]
      points[p][1] -=bbCut[0][1]
  
  pointToTuple= []
  for p in range(len(points)):
    pointToTuple.append((points[p][0], points[p][1]))
  
  imgWidth = float(rows)
  imgHeight = float(cols)
  
  img = Image.new('L', (int(imgWidth), int(imgHeight)), 0) 
  draw = ImageDraw.Draw(img)
  
  draw.polygon(pointToTuple, fill=1)
  
  array =  np.reshape(list(img.getdata()), (cols, rows))
  
  ############This is the result from the array############
  ##If you compare this array to the coloured scaled image ive have drawn
  ##its missing a 1 on the second value in the first row
  ##and another 1 on the second row 3rd value
  ##I'm assuming there is some parsing happening here with float to int?                       
  array([1, 0, 0, 0])
  array([1, 1, 0, 0])
  array([1, 1, 1, 1])
  array([1, 1, 1, 1])
  #########################################################
  
  def getPythonBoundBox(points):
      bigNumber = 10e10
      xmin = bigNumber
      xmax = -bigNumber
      ymin = bigNumber
      ymax = -bigNumber
      g = []
      a = len(points)
      for i in xrange(a):
          if points[i][0] &lt; xmin: xmin = points[i][0]
          if points[i][0] &gt; xmax: xmax = points[i][0]
          if points[i][1] &lt; ymin: ymin = points[i][1]
          if points[i][1] &gt; ymax: ymax = points[i][1]
      p1 = [xmin,ymin]
      g.append(p1)
      p2 = [xmax,ymax]
      g.append(p2)
      return (g)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/h1H0u.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/h1H0u.jpg" alt="polygon shape over grid"></a></p>