Skip to content

Instantly share code, notes, and snippets.

@sausheong
Created December 28, 2019 12:59
Show Gist options
  • Select an option

  • Save sausheong/f3443630fffbf4679713c44b95624ec5 to your computer and use it in GitHub Desktop.

Select an option

Save sausheong/f3443630fffbf4679713c44b95624ec5 to your computer and use it in GitHub Desktop.
photo mosaic blog 11
for y := bounds.Min.Y; y < bounds.Max.Y; y = y + tileSize {
for x := bounds.Min.X; x < bounds.Max.X; x = x + tileSize {
// use the top left most pixel color as the average color
r, g, b, _ := original.At(x, y).RGBA()
color := [3]float64{float64(r), float64(g), float64(b)}
// get the closest tile from the tiles DB
nearest := nearest(color, &db)
file, err := os.Open(nearest)
if err == nil {
img, _, err := image.Decode(file)
if err == nil {
// resize the tile to the correct size
t := resize(img, tileSize)
tile := t.SubImage(t.Bounds())
tileBounds := image.Rect(x, y, x+tileSize, y+tileSize)
// draw the tile into the mosaic
draw.Draw(newimage, tileBounds, tile, sp, draw.Src)
} else {
fmt.Println("error:", err, nearest)
}
} else {
fmt.Println("error:", nearest)
}
file.Close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment