How To Import Multiple Bands From An Image Into Numpy?
I'm new to python/numpy. I need to import n bands of data (~125) from a multiband image into an n-dimensional array. Each value is a 16-bit signed integer. Currently I have python
Solution 1:
GDAL already returns a Numpy array, so wrapping np.array
is unnecessary.
If you want to read all bands in the dataset, you can skip selecting the bands one at a time and use:
data = ds.ReadAsArray()
The first dimension of the array are the bands (check with print(data.shape)
).
Solution 2:
Here is the process of taking multiple band using gdal
import gdal
import numpy as np
dataset= gdal.Open(r"AnnualCrop_1.tif")
myarray1 = np.array(dataset.GetRasterBand(number of bands you want ).ReadAsArray()
Solution 3:
You should be able to loop through all the bands in a raster dataset as a numpy array. You can get the number of bands in gdal dataset using GDALDataset.RasterCount
.
For example, this would read data from each band into an array.
ds = gdal.Open("path to some file")
for band inrange( ds.RasterCount ):
band += 1
nparray = ds.GetRasterBand(band).ReadAsArray()
If you want to store the data in an array, you could initialize a 3d array with then number of rows, columns, and bands in the dataset.
Post a Comment for "How To Import Multiple Bands From An Image Into Numpy?"