title: Create numPy array from raster image
body: <p>I'm trying to turn a 4band (RGB &amp; nr Infrared) raster image into a numPy array in ArcMap. Once successfully converted to an numpy array I want to count the number of pixels that have no data on the image. When inspected in ArcMap these pixel(s) colour is marked as "None", they appear black, but they are missing either red, green and/or blue channel data from band 1,2 or 3. I need to find them.</p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>import numpy
  import os
  
  myDir = "C:\\Temp\\temp"
  # myFile = "4_pixel_test.tif"
  myFile = "4band.tif"
  
  # import 4band (R,G,B &amp; nr Infrared) image
  fName = os.path.join(myDir, myFile)
  head, tail = os.path.split(fName)
  
  
  # Convert Raster to Array, Default using LowerLeft as Origin
  rasArray = arcpy.RasterToNumPyArray(fName)
  
  # find out the number of bands in the image
  nbands = rasArray.shape[0] # int
  # print nbands (int)
  
  blackCount = 0 # count the black pixels per image
  th = 0 # Threhold value
  
  # print rasArray
  
  r, g, b, a = rasArray # not working
  
  rCheck = numpy.any(r &lt;= th)
  gCheck = numpy.any(g &lt;= th)
  bCheck = numpy.any(b &lt;= th)
  aCheck = numpy.any(a == 0)
  
  print rCheck
  print gCheck
  print bCheck
  print aCheck
  
  
  # show the results
  if rCheck:
    print ("Black pixel (red): %s" % (tail))
  
  elif gCheck:
    print ("Black pixel (green): %s" % (tail))
  
  elif bCheck:
    print ("Black pixel (blue): %s" % (tail))
  
  else:
    print ("%s okay" % (tail))
  
  if aCheck:
    print ("Transparent pixel: %s" % (tail))
  </code></pre>
  
  <blockquote>
    <blockquote>
      <blockquote>
        <p>Runtime error 
        Traceback (most recent call last):
          File "", line 14, in 
          File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy__init__.py", line 1814, in RasterToNumPyArray
            return _RasterToNumPyArray(*args, **kwargs)
        RuntimeError: ERROR 999998: Unexpected Error.</p>
      </blockquote>
    </blockquote>
  </blockquote>
  
  <pre><code># previous code which might have incorrect numpy import
  # options so I'm going with default options until I know better
  # import numpy
  # import os
  # 
  # myDir = "C:\\Temp\\temp"
  # myFile = "4_pixel_test.tif"
  # fName = os.path.join(myDir, myFile)
  # 
  # Convert Raster to Array
  # rasArray = arcpy.RasterToNumPyArray(fName)
  # maxVal = rasArray.max()
  # minVal = rasArray.min()
  # maxValpos = numpy.unravel_index(rasArray.argmax(),rasArray.shape) 
  # minValpos = numpy.unravel_index(rasArray.argmin(),rasArray.shape)
  # 
  # desc = arcpy.Describe(fName)
  # utmX = desc.extent.upperLeft.X + maxValpos[0]  
  # utmY = desc.extent.upperLeft.Y - maxValpos[1]  
  # 
  # for pixel in numpy.nditer(rasArray):
  #   # r,g,b = pixel # doesn't work  - single dimension array
  #   print pixel
  # 
  </code></pre>
  
  <p>I was able to change the raster image into numPY array from the code <a href="https://geonet.esri.com/thread/81442" rel="nofollow">here</a>.</p>
  
  <p>Not sure how the numPY array gets stored, but when iterating through it the data gets printed out starting with the y axis and working down (column by column) of the image instead of the x (row by row). </p>
  
  <p>I need to switch this so I can read the data pixel by pixel (RGBA) from top left to bottom right. However, I don't know enough about numPy to do this. </p>
  
  <p>I think the error in question maybe due to the size of the tiff in question: It works fine with 2.5MB but falls over at 4GB. :(</p>