将.png文件转换为pandas DataFrame

时间:2018-02-18 10:44:13

标签: python image pandas numpy cv2

我有一个512x512 .png雷达图像名称png_image

我还有一个带有以下标题rainfall_colour_table的csv B, G, R, rainfall_mm,可用于将颜色值与降雨强度值相关联。

阅读png_image并将其转换为512x512 Pandas DataFrame的最佳方式是什么,其中rainfall_mm的值为rainfall_colour_table

我使用了以下方法。

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]

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

0 个答案:

没有答案