Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 0.81 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import numpy as np
  2. from osgeo import gdal
  3.  
  4. def progress(n, interval=100):
  5.     "Print progress at regular intervals"
  6.     # don't want to divide by zero on first try
  7.     if n > 0:
  8.         if n % interval == 0:
  9.             print "Parsing item %i" % n
  10.     return
  11.  
  12. def image2indexed(fin, fout):
  13.     """Convert image to text, with image row index as first element of each line."""
  14.     print "\nLoading file %s" % fin
  15.     fp = gdal.Open(fin)
  16.     fp_out = open(fout, "w")
  17.     arr = fp.ReadAsArray()
  18.    
  19.     print "Parsing file"
  20.     for row in xrange(arr.shape[0]):
  21.         # this will create a string of the array row
  22.         line = " ".join(arr[row, :].astype(str).tolist())
  23.        
  24.         # this writes a line that starts with the row number
  25.         fp_out.write(" ".join([str(row), line, "\n"]))
  26.  
  27.         progress(row)
  28.     return