package main import ( "fmt" "flag" "image" "image/color" "image/draw" "image/png" "math/rand" "os" "time" ) var ( outfile = flag.String("o", "randimg.png", "Output PNG file name") ) func main() { flag.Parse() rand.Seed(time.Now().UTC().UnixNano()) out, err := os.Create(*outfile) if err != nil { fmt.Fprintf(os.Stderr, "ERROR: %v\n", err) os.Exit(1) } imgRect := image.Rect(0, 0, 200, 200) img := image.NewGray(imgRect) draw.Draw(img, img.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src) for y := 0; y < 200; y += 10 { for x := 0; x < 200; x += 10 { fill := &image.Uniform{color.Black} if rand.Intn(10) % 2 == 0 { fill = &image.Uniform{color.White} } draw.Draw(img, image.Rect(x, y, x+10, y+10), fill, image.ZP, draw.Src) } } err = png.Encode(out, img) if err != nil { fmt.Fprintf(os.Stderr, "ERROR: %v\n", err) os.Exit(2) } fmt.Printf("Wrote random image to \"%s\"\n", *outfile) os.Exit(0) }