I'm trying to remove bands from a raster I have. Basically I just want the first band as a new GeoTIFF file.
Here is my code so far:
#!/usr/local/bin/python
from osgeo import gdal
# Load my GeoTIFF
tif = gdal.Open("mygeotif.tif")
tif.RasterCount == 3 # should return True
# Create a copy with same projection, etc.
gdal.GetDriverByName("GTiff").CreateCopy("mynewgeotif.tif", tif)
newtif = gdal.Open("mygeotif_band1.tif")
# remove bands 2 and 3 here
# Close files
newtif = None
tif = None
The biggest problem is that I can't find any documentation for writing out only one band or for removing bands.
Best Answer
The next code only select the number 3 band (blue band) in a RGB raster and write it as blueband.tif.
I tried out the code with the RGB raster of the next image:
After running the code (at the Python Console of QGIS), I loaded the blue_band raster; as it can be observed at the next image:
You can adapt it for your particular purpose (customizing number bands, paths, etc).