Created
January 12, 2021 09:04
-
-
Save jamesliu96/cb9cb2365717f99f7c04739aeb08cecc 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 ( | |
"fmt" | |
"image" | |
"image/draw" | |
"image/png" | |
"math/rand" | |
"os" | |
"runtime" | |
"sync" | |
) | |
const size = 18 | |
const offset = size / 2 | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
wg := sync.WaitGroup{} | |
defer wg.Wait() | |
for i := 1; i <= 526; i++ { | |
wg.Add(1) | |
go func(i interface{}) { | |
defer wg.Done() | |
name := fmt.Sprintf("%05d.png", i) | |
src, err := open(name) | |
if err != nil { | |
panic(err) | |
} | |
dst := image.NewRGBA(src.Bounds()) | |
bounds := dst.Bounds() | |
width, height := bounds.Dx(), bounds.Dy() | |
draw.Draw(dst, bounds, image.Black, image.Point{}, draw.Src) | |
for i := 0; i < width*height; i++ { | |
x, y := rand.Intn(width), rand.Intn(height) | |
draw.Draw(dst, image.Rect(x-offset, y-offset, x+offset, y+offset), image.NewUniform(src.At(x, y)), image.Point{}, draw.Src) | |
} | |
if err = save(name, dst); err != nil { | |
panic(err) | |
} | |
}(i) | |
} | |
} | |
func open(name string) (image.Image, error) { | |
f, err := os.Open(fmt.Sprintf("seq/%s", name)) | |
if err != nil { | |
return nil, err | |
} | |
defer f.Close() | |
src, _, err := image.Decode(f) | |
if err != nil { | |
return nil, err | |
} | |
return src, nil | |
} | |
func save(name string, img image.Image) error { | |
path := fmt.Sprintf("gen/%s", name) | |
f, err := os.Create(path) | |
if err != nil { | |
return err | |
} | |
defer f.Close() | |
if png.Encode(f, img) != nil { | |
return err | |
} | |
fmt.Println("->", path) | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment