[Image-SIG] Remove Noise from a National Weather Service Radar Image (.gif)

Fredrik Lundh fredrik at pythonware.com
Sat Apr 18 22:25:54 CEST 2009

On Sat, Apr 18, 2009 at 4:54 PM, J.D. Main <jdmain at comcast.net> wrote:

> Here's a URL explaining all this:  http://radar.weather.gov/GIS.html
> In short these images are GIF files with a 256 color pallete.  The
> "interesting" pixels in these images are red, green, yellow and blue.
> The pixels representing radar "noise" usually appear as brown, grey, and
> purple.
> My desire is to iterate over all pixels and remove the noise.  The pseudo
> code would look something like this:
> for pixel in radar_image:
>   if pixel is noise:
>      turn pixel white
>   else:
>      do nothing
> I'm having a hard time with this.  Can anyone provide some insight into a
> method?  It would be greatly appreciated.

Given that GIF images are 8-bit images with a color palette (that is,
the pixel values are indexes into a color palette), here's a better

   im = Image.open("radar-image.gif")

   palette = im.getpalette()

   # prepare lookup table
   lut = range(256)
   for i in range(256):
       r, g, b = palette[i*3:i*3+3]
       if is_noise(r, g, b):
           lut[i] = 0 # or whatever index you want for the background

   # map all noise colors to 0, leave rest as is
   im = im.point(lut)

The getpalette function returns the color palette as a 768-item list
containing [red0, green0, blue0, red1, green1, ...].  The is_noise
function checks if the given RGB value corresponds to a noise color
(you have to write this one yourself).

Note that if the "noise" colors occupy a given index range, you can
skip the palette test and just set lut[i] to the background index if i
is in that range.


More information about the Image-SIG mailing list