Explorar o código

Code baseline for grabbing gifs, parsing in main

I'm trying to find the right rgb values for what is considerably a "hit"... then from there we'll grab the x, y and attempt to detect what's apart of a group, then determine direction for the group, then determine roughtly where NPR is on a x, y position, then woot woot.
Steve Thielemann hai 1 ano
pai
achega
63ff874fb2
Modificáronse 6 ficheiros con 191 adicións e 0 borrados
  1. 5 0
      .gitignore
  2. 91 0
      gif_grabber.go
  3. 5 0
      go.mod
  4. 2 0
      go.sum
  5. 73 0
      main.go
  6. 15 0
      utils.go

+ 5 - 0
.gitignore

@@ -0,0 +1,5 @@
+*.gif
+*.png
+*.json
+*.log
+

+ 91 - 0
gif_grabber.go

@@ -0,0 +1,91 @@
+package main
+
+import (
+	"fmt"
+	"image/gif"
+	"io"
+	"net/http"
+	"os"
+)
+
+func GrabPascoGif() error {
+	resp, err := http.Get("https://cdn.tegna-media.com/wtsp/weather/radar/880x495/pasco30.gif")
+	if err != nil {
+		return fmt.Errorf("http.Get('%s') => %v", "https://cdn.tegna-media.com/wtsp/weather/radar/880x495/pasco30.gif", err)
+	}
+	defer resp.Body.Close()
+	file, err := os.Create("pasco.gif")
+	if err != nil {
+		return fmt.Errorf("os.Create('pasco.gif') => %v", err)
+	}
+	defer file.Close()
+	_, err = io.Copy(file, resp.Body)
+	if err != nil {
+		return fmt.Errorf("io.Copy => %v", err)
+	}
+	return nil
+}
+
+func GrabCentralFloridaGif() error {
+	resp, err := http.Get("https://cdn.tegna-media.com/wtsp/weather/radar/880x495/central_florida.gif")
+	if err != nil {
+		return fmt.Errorf("http.Get('%s') => %v", "https://cdn.tegna-media.com/wtsp/weather/radar/880x495/central_florida.gif", err)
+	}
+	defer resp.Body.Close()
+	file, err := os.Create("central_florida.gif")
+	if err != nil {
+		return fmt.Errorf("os.Create('central_florida.gif') => %v", err)
+	}
+	defer file.Close()
+	_, err = io.Copy(file, resp.Body)
+	if err != nil {
+		return fmt.Errorf("io.Copy => %v", err)
+	}
+	return nil
+}
+
+func DecodeGif(filename string) (*gif.GIF, error) {
+	file, err := os.Open(filename)
+	if err != nil {
+		return nil, fmt.Errorf("os.Open('%s') => %v", filename, err)
+	}
+	defer file.Close()
+	g, err := gif.DecodeAll(file)
+	if err != nil {
+		return nil, fmt.Errorf("gif.DecodeAll => %v", err)
+	}
+	return g, nil
+}
+
+func EncodeGif(filename string, g *gif.GIF, separate ...bool) error {
+	sep := false
+	if len(separate) != 0 {
+		sep = separate[0]
+	}
+	if !sep {
+		file, err := os.Create(filename + ".gif")
+		if err != nil {
+			return fmt.Errorf("os.Create('%s') => %v", filename, err)
+		}
+		defer file.Close()
+		err = gif.EncodeAll(file, g)
+		if err != nil {
+			return fmt.Errorf("gif.EncodeAll => %v", err)
+		}
+		return nil
+	}
+	for idx, frame := range g.Image {
+		file, err := os.Create(fmt.Sprintf("%s-%d.png", filename, idx))
+		if err != nil {
+			return fmt.Errorf("os.Create('%s-%d.png') => %v", filename, idx, err)
+		}
+		defer file.Close()
+		err = gif.Encode(file, frame, &gif.Options{
+			NumColors: 256,
+		})
+		if err != nil {
+			return fmt.Errorf("gif.Encode(%d) => %v", idx, err)
+		}
+	}
+	return nil
+}

+ 5 - 0
go.mod

@@ -0,0 +1,5 @@
+module git.red-green.com/david/go-radar
+
+go 1.21.0
+
+require git.red-green.com/david/point2d v1.0.1 // indirect

+ 2 - 0
go.sum

@@ -0,0 +1,2 @@
+git.red-green.com/david/point2d v1.0.1 h1:E9VbOj68fIsGN8erBz1Juy/WMexYxGasj4h8D+XkTxo=
+git.red-green.com/david/point2d v1.0.1/go.mod h1:A0psWOXN81tQhgw8QuRFPsSc3UsnSehzjMicg89PLxE=

+ 73 - 0
main.go

@@ -0,0 +1,73 @@
+package main
+
+import (
+	"fmt"
+	"image"
+	"image/color"
+	"image/gif"
+	"log"
+)
+
+func main() {
+	var (
+		err        error
+		gif        *gif.GIF
+		size       image.Point
+		pix        color.Color
+		r          uint32
+		g          uint32
+		b          uint32
+		hits       uint32
+		total_hits uint64
+	)
+	/*
+		err = GrabPascoGif()
+		if err != nil {
+			log.Panic("GrabPascoGif() >", err)
+		}
+		err = GrabCentralFloridaGif()
+		if err != nil {
+			log.Panic("GrabCentralFloridaGif() >", err)
+		}
+	*/
+
+	gif, err = DecodeGif("pasco.gif")
+	if err != nil {
+		log.Panic("DecodeGif(\"pasco.gif\") >", err)
+	}
+
+	total_hits = 0
+	for frame_num, frame := range gif.Image {
+		size = frame.Bounds().Size()
+		hits = 0
+		for y := range make([]byte, size.Y) {
+			for x := range make([]byte, size.X) {
+				pix = frame.At(x, y)
+				r, g, b, _ = pix.RGBA()
+				r /= 255
+				g /= 255
+				b /= 255
+				// Now to determine if this pixel is considerable as rain
+				if r <= 140 && g >= 120 && b <= 140 {
+					//fmt.Printf("{(%d, %d) [%d, %d, %d]} ", x, y, r, g, b)
+					frame.Set(x, y, color.RGBA{R: 255, G: 0, B: 0, A: 255})
+					hits += 1
+				}
+			}
+		}
+		fmt.Printf("%s-%d: %d hits\r\n", "pasco.gif", frame_num, hits)
+		total_hits += uint64(hits)
+	}
+	fmt.Printf("%s: %d total hits\r\n", "pasco.gif", total_hits)
+
+	err = EncodeGif("out", gif)
+	if err != nil {
+		log.Panic("EncodeGif(\"out\", gif) >", err)
+	}
+	/*
+		err = EncodeGif("out", gif, true)
+		if err != nil {
+			log.Panic("EncodeGif(\"out\", gif, true) >", err)
+		}
+	*/
+}

+ 15 - 0
utils.go

@@ -0,0 +1,15 @@
+package main
+
+import "fmt"
+
+func uintToHex(v uint32) string {
+        r := fmt.Sprintf("%X", v)
+        if len(r) >= 2 {
+                return r[0:2]
+        }
+        return r
+}
+
+func rgbToHex(r, g, b uint32) string {
+        return fmt.Sprintf("%s%s%s", uintToHex(r), uintToHex(g), uintToHex(b))
+}