I have a 512x512 .png radar image name png_image
I also have a csv rainfall_colour_table
with the following headers B, G, R, rainfall_mm
, that I can use to link colour values to rainfall intensity values.
What is the best way to read png_image
and convert it to a 512x512 Pandas DataFrame where the values are rainfall_mm
from rainfall_colour_table
.
I have used the following approach.
from urllib.request import urlopen, Request
import cv2
import os
import numpy as np
import pandas as pd
# NOTE: the ftp sourve of the png changes every 30 min
# get any current .png file name from the website ftp://ftp.bom.gov.au/anon/gen/radar//
# and set the below variable
file_name =
# get connection to file
input_url = "ftp://ftp.bom.gov.au/anon/gen/radar//" + file_name
req = Request(input_url)
req_html = urlopen(req).read()
# read file
radar_image = np.fromstring(req_html, np.uint8) # read byte image
radar_image = cv2.imdecode(radar_image, cv2.IMREAD_COLOR) # convert to numppy array
# OS agnostic relative file path
# get the current directory path
base_dir = os.path.dirname(__file__)
# OS agnostic relative file path
# load colour to mm/hr concurrency table
rainfall_colour_table = os.path.join(os.sep, base_dir, 'sample_data', 'radar_colours.csv')
rainfall_colour_df = pd.read_csv(rainfall_colour_table)
rainfall_colour_df.set_index(['B', 'G', 'R'], inplace=True)
# switch colours with rain intensity
radar_df = pd.DataFrame(rainfall_colour_df.loc[list(map(tuple, pin))].rainfall.values for pin in radar_image)
radar_df.columns = ['pixel_col_' + str(col) for col in radar_df.columns]
The rainfall_colour_table.csv
colour_id,rainfall,B,G,R
2,1.5,255,180,180
3,2.5,255,120,120
4,4,255,20,20
5,6,195,216,0
6,10,144,150,0
7,15,102,102,0
8,20,0,255,255
9,35,0,200,255
10,50,0,150,255
11,80,0,100,255
12,120,0,0,255
13,200,0,0,200
14,300,0,0,120
15,360,0,0,40