已有氣象接口,須要後端渲染生成圖片。已經使用python3.7實現一個版本Python 氣象數據渲染成圖片,可以知足需求功能,也發現以下幾個問題:python
exe
文件特別大:由於我引用一些第三方包,在打包時達到300M,一個小工具這是不能接受的api-ms-win-crt-runtime-l1-1-0.dll 丟失
,而且還須要按照python環境。生成圖片的核心代碼golang
package main
import (
"image"
"image/color"
"image/color/palette"
"image/png"
"os"
)
type WeatherData struct {
code int
msg string
rows int
cols int
values [10][50] float32
}
func main() {
var wdatas WeatherData
wdatas.code = 200
wdatas.msg = "success"
wdatas.rows = 10
wdatas.cols = 50
var tvals = [10][50]float32{
{0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1, 0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1, 0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1, 0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1, 0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1},
{0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1, 0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1, 0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1, 0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1, 0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1},
{0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1, 0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1, 0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1, 0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1, 0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1},
{0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1, 0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1, 0, 0, 0.1, 0.5, 0.9, 0.7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0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1, 0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1},
{0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1, 0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1, 0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1, 0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1, 0, 0, 0.1, 0.5, 0.9, 0.7, 0, 0.6, 0, 1},
}
wdatas.values = tvals
const (
w, h int = 256, 256
R float64 = 50
// 大約一秒30幀
frameN int = 30
delay int = 100 / frameN
)
// 216色 + 透明
var palette = append(palette.WebSafe, color.Transparent)
var width = wdatas.cols
var height = wdatas.rows
img := image.NewPaletted(image.Rect(0, 0, width, height), palette)
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
print(wdatas.values[y][x], " ")
if wdatas.values[y][x] > 0.0 {
img.Set(x, y, color.RGBA{255, 1, 1, 255})
} else {
img.Set(x, y, color.Transparent)
}
//img.Set(x, y, color.Transparent)
}
println()
}
// 建立文件
file2, err := os.Create("test04.png")
if err != nil {
println("error...")
}
// 使用png格式將數據寫入文件
png.Encode(file2, img) //將image信息寫入文件中
// 關閉文件
file2.Close()
}
複製代碼
go build ***.go
打包,生成能夠執行文件7.79M