Skip to content

Instantly share code, notes, and snippets.

@greencoder
Created September 30, 2014 14:24
Show Gist options
  • Save greencoder/27e48ea79ca3e264105c to your computer and use it in GitHub Desktop.
Save greencoder/27e48ea79ca3e264105c to your computer and use it in GitHub Desktop.
NOAA RIDGE Radar fringe remover
import numpy
import requests
import StringIO
# Use Pillow
from PIL import Image
# Get the latest radar image
request = requests.get('http://radar.weather.gov/ridge/Conus/RadarImg/latest_radaronly.gif')
# Get the binary content from the HTTP response
image_data = request.content
# Convert the response data into an image, then feed that into numpy
im = Image.open(StringIO.StringIO(image_data))
im = im.convert('RGBA')
data = numpy.array(im)
# These are all the RGB colors we want to strip
noisy_colors = (
(255, 255, 255), # White
(225, 225, 225), # Light Gray
(227, 227, 227), # Light Gray
(230, 230, 230), # Light Gray
(232, 232, 232), # Light Gray
(235, 235, 235), # Light Gray
(238, 238, 238), # Light Gray
(240, 240, 240), # Light Gray
(004, 233, 231), # Cyan
(001, 159, 244), # Light Blue
(000, 034, 240), # Dark Blue
(003, 000, 244), # Dark Blue
)
# Loop through the noisy colors and find any pixels with that
# color value. When found, turn the alpha channel to zero
# to make that pixel transparent
for color in noisy_colors:
# Get the R,G,B values for the current noisy color. Create a
# set of R,G,B,A values to convert it to if found. (The A is
# the alpha channel and we set it to zero to make it transparent)
r1, g1, b1 = color
r2, g2, b2, a2 = r1, g1, b1, 0
# This is numpy voodoo that I don't claim to understand yet. Thanks, SO:
# http://stackoverflow.com/questions/6483489/change-the-color-of-all-pixels-with-another-color
red, green, blue, alpha = data[:,:,0], data[:,:,1], data[:,:,2], data[:,:,3]
mask = (red == r1) & (green == g1) & (blue == b1)
data[:,:,:4][mask] = [r2, g2, b2, a2]
# Save the modified pixels back out to an image
im = Image.fromarray(data)
im.save('radar.png', 'PNG')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment