首頁>技術>

Golang 氣象資料渲染成圖片打包生成exe檔案特別大:因為我引用一些第三方包,在打包時達到300M,一個小工具這是不能接受的部署困難:我本地開發是win10,打包執行沒有問題;伺服器是Windows server 2008 ,在伺服器上部署時提醒api-ms-win-crt-runtime-l1-1-0.dll 丟失,並且還需要按照python環境。(注:本人不太熟悉python,可能有更好的解決方案)使用Golang生成圖片生成圖片的核心程式碼

package mainimport ("image""image/color""image/color/palette""image/png""os")type WeatherData struct {code intmsg stringrows intcols intvalues [10][50] float32}func main() {var wdatas WeatherDatawdatas.code = 200wdatas.msg = "success"wdatas.rows = 10wdatas.cols = 50var 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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 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},}wdatas.values = tvalsconst (w, h int = 256, 256R float64 = 50// 大約一秒30幀frameN int = 30delay int = 100 / frameN)// 216色 + 透明var palette = append(palette.WebSafe, color.Transparent)var width = wdatas.colsvar height = wdatas.rowsimg := 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部署:直接複製就可以部署,沒有其他問題

7
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 基於Ganos百行程式碼實現億級向量空間資料線上視覺化