Skip to content

Instantly share code, notes, and snippets.

@mikkohei13
Created October 16, 2020 21:31
Show Gist options
  • Save mikkohei13/b95076c60574d9deb4d13e48037f509f to your computer and use it in GitHub Desktop.
Save mikkohei13/b95076c60574d9deb4d13e48037f509f to your computer and use it in GitHub Desktop.
Python code to extract rain intensity colors from FMI rain radar maps.
from PIL import Image
import colorsys
def rgb2hsv(rgb):
r = rgb[0] / 255
g = rgb[1] / 255
b = rgb[2] / 255
hsvRaw = colorsys.rgb_to_hsv(r, g, b)
hsv = []
hsv.append(int(hsvRaw[0] * 360))
hsv.append(int(hsvRaw[1] * 100))
hsv.append(int(hsvRaw[2] * 100))
return hsv
def colorFunc(pixel):
waterbody = (0,0,0)
if (210,224,228) == pixel: # sea
return waterbody
if (210,224,230) == pixel: # lake
return waterbody
# HSV
hsv = rgb2hsv(pixel)
# Text antialiasing (gray)
if hsv[1] < 5:
return (0,0,0)
# Combined road, border, shore (warm-colored grayish)
if 17 < hsv[0] < 202 and hsv[1] < 30:
return (0,0,0)
# Rain colors
# blue
if 208 <= hsv[0] <= 217 and 16 <= hsv[1] <= 38:
return pixel
# blue 2
if 20 <= hsv[0] <= 206 and 44 <= hsv[1] <= 56:
return pixel
# turq 1 & 2
if 163 <= hsv[0] <= 181 and 38 <= hsv[1] <= 63:
return pixel
# green
if 82 <= hsv[0] <= 92 and 60 <= hsv[1] <= 69:
return pixel
# yellow
if 58 <= hsv[0] <= 64 and 63 <= hsv[1] <= 71:
return pixel
# orange 1
if 46 <= hsv[0] <= 51 and 64 <= hsv[1] <= 69:
return pixel
# orange 2
if 27 <= hsv[0] <= 32 and 63 <= hsv[1] <= 73:
return pixel
# red
if 5 <= hsv[0] <= 9 and 68 <= hsv[1] <= 72:
return pixel
return (0,0,0)
files = []
files.append("1.png")
files.append("2.png")
for fileNro, filename in enumerate(files):
newFilename = filename.replace(".", "_rain.")
img = Image.open(filename)
new = Image.new('RGB', (img.width, img.height))
for x in range(img.width):
for y in range(img.height):
pixel = img.getpixel((x, y))
color = colorFunc(pixel)
new.putpixel((x, y), color)
new.save(newFilename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment