Last active
October 30, 2020 19:26
-
-
Save blakelead/8799739b4d324c1950c3fb32cd7bac74 to your computer and use it in GitHub Desktop.
Generate simple random images
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 ( | |
"flag" | |
"fmt" | |
"image" | |
"image/color" | |
"image/png" | |
"log" | |
"math" | |
"math/rand" | |
"os" | |
"time" | |
) | |
var ( | |
imgSize int | |
imgCount int | |
rectSize int | |
rectExp int | |
) | |
func main() { | |
flag.IntVar(&imgSize, "img.size", 256, "ImaSize of the image in pixels.") | |
flag.IntVar(&imgCount, "img.count", 1, "Number of generated images.") | |
flag.IntVar(&rectSize, "rect.size", 64, "Size of the rectangles inside the image.") | |
flag.IntVar(&rectExp, "rect.exp", 3, "Number from 1 to MAX_INT64. Lower means more rectangles.") | |
flag.Parse() | |
imgSize = clamp(imgSize, 1, 4096) | |
imgCount = clamp(imgCount, 1, 10000) | |
rectSize = clamp(rectSize, 1, imgSize) | |
rectExp = clamp(rectExp, 1, math.MaxInt64) | |
log.Printf("Image size: %d", imgSize) | |
log.Printf("Image count: %d", imgCount) | |
log.Printf("Rectangles size: %d", rectSize) | |
log.Printf("Rectangles magic number: %d", rectExp) | |
rand.Seed(time.Now().UnixNano()) | |
for x := 0; x < imgCount; x++ { | |
generateImage(fmt.Sprintf("image_%dpx_%05d", imgSize, x+1)) | |
} | |
} | |
func generateImage(name string) { | |
img := image.NewRGBA(image.Rect(0, 0, imgSize, imgSize)) | |
bgColor := color.RGBA{uint8(rand.Int()), uint8(rand.Int()), uint8(rand.Int()), 255} | |
for y := 0; y < imgSize; y++ { | |
for x := 0; x < imgSize; x++ { | |
img.SetRGBA(x, y, bgColor) | |
} | |
} | |
rectColor := color.RGBA{uint8(rand.Int()), uint8(rand.Int()), uint8(rand.Int()), 255} | |
for y := 0; y < imgSize; y += rectSize { | |
for x := 0; x < imgSize; x += rectSize { | |
if rand.Intn(rectExp) == 0 { | |
for h := y; h < y+rectSize; h++ { | |
for w := x; w < x+rectSize; w++ { | |
img.SetRGBA(w, h, rectColor) | |
} | |
} | |
} | |
} | |
} | |
file, err := os.Create(name + ".png") | |
if err != nil { | |
log.Fatal(err) | |
} | |
err = png.Encode(file, img) | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Printf("Image created as: %s", name+".png") | |
} | |
func clamp(num, min, max int) int { | |
return int(math.Min(math.Max(float64(num), float64(min)), float64(max))) | |
} |
Author
blakelead
commented
Oct 30, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment