By HelloGoodbye

2018-09-10 16:09:59 8 Comments

I have a GeoTIFF image that contains a color table and a single raster band with 8-bit table keys, and that uses LZW compression, that I load with gdal.Open. I also have a numpy array containing 24-bit RGB-values (for a blurred version of the image), corresponding to three 8-bit raster bands. I need to substitute these three raster bands for the raster band that is currently in the image, and then save the image (preferably as a new file if possible). How do I do that?

I would like to keep the data in the numpy array in RGB form, so I would like to end up with three raster band instead of one. I see that there is an AddBand method, but how do I remove an existing band (or modify it, since the bands happen to have the same bit-depth)? Also, if I make the image contain three bands instead of just one, do I need to do something more to specify that the three bands represent R, G and B?


@Val 2018-09-14 10:56:55

The way I would do it, to just create a fresh copy of your template raster with the new values ... If you want to avoid having copies at all costs, you could also overwrite. But creating a copy is less error prone, and you can retain the original.

This function assumes that you have an array arr, which has three bands in the third dimension (so 2nd axis).

import gdal

def createRGB(template,arr,filename):
    '''Creates a copy of a 3-band raster with values from array


        template: Path to template raster
        arr: Value array with dimensions (r,c,3)
        filename: Output filename for new raster 

    # Open template
    t = gdal.Open(template)

    # Get geotiff driver
    driver = gdal.GetDriverByName('GTiff')

    # Create new raster
    r = driver.Create(filename, t.RasterXSize, t.RasterYSize, 3, gdal.GDT_Byte,['COMPRESS=LZW'])

    # Set metadata

    # loop through bands and write new values
    for bix in range(3):

        rb = self.raster.GetRasterBand(bix+1)

        # Write array

    # Close datasets
    t = None
    r = None
    rb = None

Related Questions

Sponsored Content

63 Answered Questions

[SOLVED] Calling an external command from Python

13 Answered Questions

[SOLVED] How can I make a time delay in Python?

23 Answered Questions

[SOLVED] Does Python have a ternary conditional operator?

16 Answered Questions

[SOLVED] What are metaclasses in Python?

42 Answered Questions

[SOLVED] How do I merge two dictionaries in a single expression?

37 Answered Questions

[SOLVED] How do I check whether a file exists without exceptions?

36 Answered Questions

[SOLVED] How to get the current time in Python

  • 2009-01-06 04:54:23
  • user46646
  • 3095605 View
  • 2648 Score
  • 36 Answer
  • Tags:   python datetime time

10 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

11 Answered Questions

[SOLVED] How do I get a substring of a string in Python?

  • 2009-03-19 17:29:41
  • Joan Venge
  • 2740724 View
  • 2010 Score
  • 11 Answer
  • Tags:   python string

25 Answered Questions

[SOLVED] How can I safely create a nested directory?

Sponsored Content