[Gdal-dev] Newbie needs help with RGB images

Christopher Barker Chris.Barker at noaa.gov
Fri Dec 1 12:32:10 EST 2006


opcode at telus.net wrote:
> I'm just getting started with GDAL in my code( 1 day under my belt :-) ) and 
> am looking for some direction on how to load a GeoTiff (or other ) image and 
> convert it to a RGB image buffer. The tiff image is 20,000 x 24000 x 3 and I 
> want to downsample it into a 1024 x 1024 x 3 RGB image. 

> I am thinking I can get it one band at a time with the dataset->RasterIO 
> method and changing the offset each pass but this is likely to be very slow as 
> it will downsample the entire image for each band. Is there any way to 
> directly create the interleaved RGB?

As far as I could figure out, you need to do each band separately, but 
that doesn't make it that slow -- there's only three bands.

RasterBand.ReadRaster takes new dimensions as parameters, so the down 
sampling is easy and one step. Here's some Python code that works for me:

This puts the RBG data in a numpy array
        ## XY is the coords of the corner of the part of the image you
        ## want, (0,0) if you want the whole thing
        ## WH is the size of the piece of the image you want:
        ##   (Width, Height)
        ## NewWH is the size you want the ouput image at:
        ##   (Width, Height)
        DataRect = N.zeros((NewWH[0],NewWH[1],3), N.uint8)
        for i in range(3):
             Band = self.Dataset.GetRasterBand(i+1)
             color = Band.ReadRaster( XY[0], # nXOff
                                      XY[1], # nYOff
                                      WH[0], # nXSize
                                      WH[1], # nYSize
                                      NewWH[0], #nBufXSize
                                      NewWH[1], #nBufYSize
                                      GDT_Byte, # data type
                                      )
             # put it in the numpy array:
             DataRect[:,:,i] = N.fromstring(color,N.uint8).reshape(NewWH)


If you are using Python, and either PIL or wxPython, I can send you more 
code that might be helpful.

-Chris

-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov



More information about the Gdal-dev mailing list