[gdal-dev] trying to change regionator image resampling to nearest-neighbor

Mary Jo Brodzik brodzik at nsidc.org
Thu Nov 12 14:32:55 EST 2009

Dear gdal-dev list members,

I'm using a python tool called the regionator 
(http://code.google.com/p/regionator/wiki/ Welcome) on a large 
(14352x7440) png image.  The regionator uses the GDAL library to resample 
subsets of the original image into much smaller chunk images.  These 
images are set up at increasing levels of detail in a kml file that make 
the download of this information into a googleEarth client much more 

The png is color and includes transparency; its approximate resolution is 
500 meters. I basically have 4 colors in the original image, black, white, 
blue and yellow. The output images that regionator is producing are great, 
but when I zoom in to distances where the original pixel sizes are 
visible, it looks like the resampling did some averaging across the 4 
colors, so I'm getting blue sections surrounded by a blue halo that bleeds 
into white, and yellow and blue values bleeding into each other, etc.

I'd like to control the resampling, and force it to do nearest-neighbor 
rather than averaging to eliminate the halos.  It looks to me like the 
regionator is doing the resampling with calls to ReadRaster and 
WriteRaster in in a class called extractor.py, but I don't know enough 
about the GDAL libraries to know how to change them. Is it possible to 
force the resampling to be nearest neighbor?  extractor.py isn't terribly 
large or complicated, so I'm including it here (below).

I've already tried the googleearth forum and the kml-developers forum, no 
help from those communities.  Python's new to me, and I've only used the 
GDAL command-line utilities up to now.

Thanks in advance for any tips.

Mary Jo

Here's the regionator's extractor.py:

Copyright (C) 2006 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at


Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.

$URL: http://regionator.googlecode.com/svn/trunk/kml/extractor.py $
$Revision: 200 $
$Date: 2007-01-12 09:12:48 -0700 (Fri, 12 Jan 2007) $

""" class Extractor

Phase 3 of SuperOverlay creation.

Extract tiles from an image

Front-ends GDAL.  Input file and output driver must be
some format known to GDAL.


import gdal
import tempfile
import os

class Extractor:

   """ class Extractor

   Create for Extractor the image file.
   Each tile is resampled to the specified size and written
   to a file of the given format.


   def __init__(self,imgfile,twid,tht,fmt,verbose=False):


       imgfile: a gtiff
       twid,tht: extracted tile resample pixel width/height
       fmt: GDAL output driver name ('PNG','JPEG')

     self.__in_ds = gdal.Open(imgfile)
     self.__twid = twid
     self.__tht = tht
     self.__fmt = fmt
     self.__verbose = verbose

     self.__bands = self.__in_ds.RasterCount

     if self.__verbose:
       print 'Extractor %s %d bands' % (imgfile,self.__bands)

     # Intermediate work must be in GTiff (?)
     self.__gtiff_driver = gdal.GetDriverByName('GTiff')

     # Output driver/format is whatever the user specifies
     # XXX handle bad/wrong fmt's less gracelessly
     self.__o_driver = gdal.GetDriverByName(fmt)

   def Extract(self,x,y,wid,ht,basename):

     """ Extract a tile into a file

     The given tile is extracted, resampled and saved
     according to the tile pixel dimensions and format
     specified at object __init__().

       x,y: pixel offset
       wid,ht: pixel dimensions

       True: complete success
       False: any failure


     # Get the tile's pixels resampled
     twid = self.__twid
     tht = self.__tht
     i_data = self.__in_ds.ReadRaster(x,y,wid,ht,buf_xsize=twid,buf_ysize=tht)

     # Have to Create out to GTiff first (?)
     (fd, tmpfile) = tempfile.mkstemp(suffix='GTiff')
     o_ds = self.__gtiff_driver.Create(tmpfile,twid,tht,bands=self.__bands)

     # Save off using the specified driver
     filename = '%s.%s' % (basename, self.__fmt)
     options = []
     if self.__fmt == 'JPEG':
       options.append('QUALITY=90') # Default of 75 is low quality
     self.__o_driver.CreateCopy(filename, o_ds, options=options)

     if self.__verbose:
       print filename

     # Delete the tmp file

More information about the gdal-dev mailing list