Search code examples
pythonarraystiffgdal

creating an array from a TIF


I am trying to create an array from a single band TIF image using GDAL: example

array= band4.ReadAsArray(0,0,xsize,ysize)

but it comes out as only zeros?

array
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
 ...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]

Is there a way around this or another way to create the array? The image is unsigned 16 bit...


Solution

  • As @the_cheff stated in the comments, it is not necessary to pass any parameter when calling band.ReadAsArray(). Calling just the function will return a numpy array of the whole band.

    The function structure is as follows:

    band.ReadAsArray([xoff], [yoff], [win_xsize], [win_ysize], [buf_xsize], [buf_ysize], [buf_obj])

    where,

    • xoff is the column to start reading at. Default is 0 (first column).
    • yoff is the row to start reading at. Default is 0 (first row).
    • win_xsize is the number of columns to read. Default is to read them all.
    • win_ysize is the number of rows to read. Default is to read them all.
    • buf_xsize is the number of columns in the output array. Default is to use the win_xsize value. Data will be resampled if this value is different than win_xsize.
    • buf_ysize is the number of rows in the output array. Default is to use the win_ysize value. Data will be resampled if this value is different than win_ysize.
    • buf_obj is a NumPy array to put the data into instead of creating a new array. Data will be resampled, if needed, to fit into this array. Values will also be converted to the data type of this array.

    I would confirm that the band has indeed values different than 0 and that it is the band you're trying to read. In some cases the band #4 is used as an alpha channel with values that range from 0 to 1.