Skip to content

ReadImage15Python

anna-dodd edited this page Jun 3, 2015 · 1 revision
Below is the nitf_extract.py test application that comes with the NITRO software.

from nitf import *
import os, sys, logging, glob

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(levelname)s %(message)s')


def extract_image(subheader, index, imageReader, outDir=None, baseName=None):
    window = SubWindow()
    window.numRows = subheader['numRows'].intValue()
    window.numCols = subheader['numCols'].intValue()
    window.bandList = range(subheader.getBandCount())
    nbpp = subheader['numBitsPerPixel'].intValue()
    bandData = imageReader.read(window)
   
    if not outDir: outDir = os.getcwd()
    if not baseName: baseName = os.path.basename(os.tempnam())
   
    outNames = []
    for band, data in enumerate(bandData):
        outName = '%s_%d__%d_x_%d_%d_band_%d.out' % (
             baseName, index, window.numRows, window.numCols, nbpp, band)
        outName = os.path.join(outDir, outName)
        f = open(outName, 'wb')
        f.write(data)
        f.close()
        outNames.append(outName)
        logging.info('Wrote band data to file %s' % outName)
    return outNames



def extract_images(fileName, outDir=None):
    if not outDir: outDir = os.getcwd()
    if not os.path.exists(outDir): os.makedirs(outDir)
   
    handle = IOHandle(fileName)
    reader = Reader()
    record = reader.read(handle)
    logging.info('Dumping file: %s' % fileName)
   
    for i, segment in enumerate(record.getImages()):
        logging.info('--- Image [%d] ---' % i)
        imReader = reader.newImageReader(i)
        extract_image(segment.subheader, i, imReader, outDir, os.path.basename(fileName))
    handle.close()


if __name__ == '__main__':
    for arg in sys.argv[1:]:
        if os.path.isfile(arg):
            extract_images(arg)
        elif os.path.isdir(arg):
            map(extract_images, glob.glob(os.path.join(arg, '*.ntf')) + glob.glob(os.path.join(arg, '*.NTF')))
Clone this wiki locally