Skip to content

Instantly share code, notes, and snippets.

@dtoebe
Created January 3, 2017 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtoebe/f0d50402c055f0ddf369edab4025fbb7 to your computer and use it in GitHub Desktop.
Save dtoebe/f0d50402c055f0ddf369edab4025fbb7 to your computer and use it in GitHub Desktop.
GENERATING DATAURI IMAGES WITH GO - main.go - 2
//main.go
package main
import (...)
func main() {
// Seed the std random number generator
rand.Seed(time.Now().UTC().UnixNano())
// We will leave out creating the png image file since we
// don't need the file
// Set the bounds of the image basically 100px width / height
imgRect := image.Rect(0, 0, 100, 100)
// This creates the image in memory, and sets all pixels to a gray value.
// And it sets the size based on the Rect we just created
img := image.NewGray(imgRect)
// Ok a lot going on here. Let's start creates a nil mask layer on the image
// Tt takes the image we created in memory, and then the bounding Rect
// Next we add a reference to a uniform color (white) over the mask
// The image.ZP means the mask we are drawing starts at the 0,0 point
// draw.Src reference the source mask
draw.Draw(img, img.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src)
// These nested loop reference the y,x coordinates.
// 100 is reference to the image with / height in pixels
// 10 is reference to the size of the random blocks to be generated
for y := 0; y < 100; y += 10 {
for x := 0; x < 100; x += 10 {
// Here we create a color to be set as a black block
fill := &image.Uniform{color.Black}
// rand.Intn(10) creates a random non-negative number between 0,10
// Then checks if it's even
if rand.Intn(10)%2 == 0 {
fill = &image.Uniform{color.White}
}
// This draw.Draw takes a new Rect that is 10px,10px and sets
// color based on fill
draw.Draw(img, image.Rect(x, y, x+10, y+10), fill, image.ZP, draw.Src)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment