I'm looking to merge 3 imagery bands that are saved as separate files. The file format is NITF. The reason they are saved as NITF is because they have RPC metadata embedded into the images. I would like to combine the NITF images so the output image retains the RPC metadata. I will be doing this a lot so I would like to build a script either through Python or GDAL so that I don't have to do this manually. Unfortunately I'm very much a novice when it comes to scripting, and after scouring the internet I have hit a road block. I was able to successfully merge the images in the desired output using GDAL, but the RPC metadata did not transfer into the output image.

Here's the code I have so far:

gdal_merge.py -o output.ntf -of NITF -seperate red_band.ntf green_band.ntf blue_band.ntf -co PHOTOMETRIC=RGB
  • 1
    Welcome to gis.SE! Please share the code that you have come up with already. Also, if you haven't done so, please take the tour. – Stefan Dec 13 '18 at 20:57
  • I added an edit with the code I have so far. Thank you for the suggestion. – David Dec 13 '18 at 21:23

This should do the trick in Python. If you don't have rasterio install I suggest to get it through conda.

import numpy as np
import rasterio

data = []
input_images = [path1, path2, path3]
for img in input_images:
    # Open the image file, then read the data in memory as np.ndarray
    img_meta = rasterio.open(img)
    img_data = img_meta.read()

# stack along the first axis all data
new_data = np.vstack(tuple(data))

# This assumes that all of them have the same crs, transform and nodata value
newprofile = {
    'count': new_data.shape[0],
    'crs': img.crs,
    'dtype': 'uint8',
    'driver': 'GTiff',  # You can pick other formats that suits you. There is read-write support for NITF if you prefer
    'transform': img.transform,
    'height': new_data.shape[1],
    'width': new_data.shape[2],
    'blockxsize': 256,
    'tiled': True,
    'blockysize': 256,
    'nodata': img.nodata 

# Initialise a new GeoTiff and write the data
with rasterio.open(outpath, 'w', **newprofile) as dst:

Regarding metadata I'm not sure how you want to combine them, but you can read them using img.tags(). This will return a dictionary, that you can write to the new image using the dst.update_tags(). Please note that this last function is only available if you open the image in 'w' or 'r+' mode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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