I am looking for any tool (or python code snippet) that can loop through a file folder of .jp2 images and determine which have 3 bands and which have 4 bands.

I am having trouble creating a raster dataset as some of them have 3 bands and others have 4. I need to isolate the band count and replace the images with only 3 bands. I am looking to output a text file that has the name of the file which has 3 bands.

Any thoughts?

1 Answer 1


GDAL can help you do this. You can use GDAL's python API

import os
from osgeo import gdal

for f in os.listdir(os.getcwd()):
  ds = gdal.Open(f)
  if ds.RasterCount == 3:
    print f
  • I will try tomorrow and see how it goes! thanks for the quick comment.
    – MDHald
    Sep 24, 2014 at 23:50
  • Can you tell me where I would place the script? does it go in the directory? e.g.P:\Data\Aerials\2010\Orthos\zip where all the files are located? or does it go in the code snippet somewhere?
    – MDHald
    Sep 25, 2014 at 13:39
  • It would go in the data directory. If you want to run it from elsewhere, replace os.getCwd() with the full path to the directory containing the rasters. Sep 28, 2014 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.