Last active
November 17, 2020 10:43
-
-
Save tomowarkar/ebf97785c497c1e22be9bd9c6c5b49c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"os" | |
"image" | |
"image/color" | |
"image/png" | |
"fmt" | |
"path" | |
"log" | |
"time" | |
) | |
type FloatMatrix [][]float64 | |
func (mat FloatMatrix) toImg(palette func(float64)color.RGBA) *image.RGBA{ | |
h, w := len(mat), len(mat[0]) | |
img := image.NewRGBA(image.Rect(0, 0, w, h)) | |
for i:=0;i<h;i++{for j:=0;j<w;j++{img.Set(j, i, palette(mat[i][j]))}} | |
return img | |
} | |
func main(){ | |
h,w := 300,500 | |
arr := make([][]float64, h) | |
for i := range arr { arr[i] = make([]float64, w) } | |
for i := range arr { | |
for j := range arr[i] { | |
arr[i][j]=float64(i+j) | |
} | |
} | |
narr := normalization(arr) | |
baseC := color.RGBA{255, 100, 100, 255} | |
destC := color.RGBA{255, 255, 100, 255} | |
img := FloatMatrix(narr).toImg(func(v float64)color.RGBA{ | |
r := float64(destC.R-baseC.R)*v+float64(baseC.R) | |
g := float64(destC.G-baseC.G)*v+float64(baseC.G) | |
b := float64(destC.B-baseC.B)*v+float64(baseC.B) | |
a := float64(destC.A-baseC.A)*v+float64(baseC.A) | |
return color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)} | |
}) | |
if err:=saveImage(img, ".", time.Now().Format(time.Stamp), ".png"); err!=nil{ | |
log.Println(err) | |
} | |
} | |
func saveImage(img *image.RGBA, destpath, filename, extention string) error { | |
fp, err := createNewFile(destpath, filename, extention) | |
if err != nil { | |
return err | |
} | |
defer fp.Close() | |
switch extention { | |
case ".png": | |
err = png.Encode(fp, img) | |
default: | |
defer os.Remove(fp.Name()) | |
return fmt.Errorf("extention %s is not supported", extention) | |
} | |
return err | |
} | |
func createNewFile(destpath, filename, extention string) (*os.File, error) { | |
if _, err := os.Stat(destpath); err != nil{ | |
return nil, err | |
} | |
basename := fmt.Sprint(filename, extention) | |
filepath := path.Join(destpath, basename) | |
if _, err := os.Stat(filepath); err == nil{ | |
return nil, fmt.Errorf("file %s is already exist", filepath) | |
} | |
f, err := os.Create(filepath) | |
if err != nil { | |
return nil, err | |
} | |
return f, nil | |
} | |
func normalization(arr [][]float64) [][]float64 { | |
h, w := len(arr), len(arr[0]) | |
mn, mx := arr[0][0], arr[0][0] | |
for i := range arr { | |
for _, e := range arr[i] { | |
mn = min(mn, e) | |
mx = max(mn, e) | |
} | |
} | |
ret := make([][]float64, h) | |
for i := range ret { ret[i] = make([]float64, w) } | |
for i := range arr { | |
for j, e := range arr[i] { | |
ret[i][j] = (e-mn)/(mx-mn) | |
} | |
} | |
return ret | |
} | |
func max(a ...float64)float64{if len(a)==0{return 0};ret := a[0];for _,e:=range a{if e>ret{ret=e}};return ret} | |
func min(a ...float64)float64{if len(a)==0{return 0};ret := a[0];for _,e:=range a{if e<ret{ret=e}};return ret} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment