Generate User's Avatars with Pure Golang
GolangWebDev
GolangWebDev
3438 0 1

Generate User's Avatars with Pure Golang

Create avatars based on names. The colors are chosen based on the first character. Here is some Avatar generation library or Code for GO language.

GOvatar

o1egl/govatar is an Avatar generation library for GO language.

Usage As lib

Generates avatar and save it to filePath

err := govatar.GenerateFile(govatar.MALE, "/path/to/avatar.jpg")
err := govatar.GenerateFileFromUsername(govatar.MALE, "username", "/path/to/avatar.jpg")

Generates an avatar and returns it as an image.Image

img, err := govatar.Generate(govatar.MALE)
img, err := govatar.GenerateForUsername(govatar.MALE, "username")

Avatar looks like:

GOvatar avatar

Cameron

Cameron is An simple avatar generator for Go, the name of this project came from the
Avatar's director
James Cameron.

Avatar looks like github avatar generator:
Cameron avatar generator

Usage

s := "demo"
sideLen := 120
buf := bytes.Buffer{}
err = png.Encode(&buf, cameron.Identicon([]byte(s), sideLen, sideLen/9))
if err != nil {
	return
}
ioutil.WriteFile(s+".png", buf.Bytes(), 0644)

letteravatar

letteravatar Letter avatar generation for Go.

Letteravatar Usage

package main

import (
	"image/png"
	"log"
	"os"
	"unicode/utf8"

	"github.com/disintegration/letteravatar"
)

var names = []string{
	"Alice",
	"Bob",
	"Carol",
	"Dave",
	"Eve",
	"Frank",
	"Gloria",
	"Henry",
	"Isabella",
	"James",
	"Жозефина",
	"Ярослав",
}

func main() {
	for _, name := range names {
		firstLetter, _ := utf8.DecodeRuneInString(name)

		img, err := letteravatar.Draw(75, firstLetter, nil)
		if err != nil {
			log.Fatal(err)
		}

		file, err := os.Create(name + ".png")
		if err != nil {
			log.Fatal(err)
		}

		err = png.Encode(file, img)
		if err != nil {
			log.Fatal(err)
		}
	}
}

Output

Letter avatar generation

Manually avatar generator

Coding with go image

There are four steps

  • Create a new RGBA image with the given bounds
  • Paint BackGround with color.RGBA
  • Splatter with username
  • Save PNG

Step by step

avatar := image.NewRGBA(image.Rect(0, 0, AvatarSize, AvatarSize))
PaintBG(avatar, CalcBGColor(nameBytes))
Splatter(avatar, nameBytes, CalcPixelColor(nameBytes))
SavePNG(avatar, string(nameBytes))

Some func

func SavePNG(avatar image.Image, name) {
	file, err := os.Create(name + ".png")
	err = png.Encode(file, avatar)

	if err != nil {
		panic(err)
	}
}

func Splatter(avatar *image.RGBA, nameBytes []byte, pixelColor color.RGBA) {

	// A somewhat random number based on the username.
	var nameSum int64
	for i := range nameBytes {
		nameSum += int64(nameBytes[i])
	}

	// Use said number to keep random-ness deterministic for a given name
	rand.Seed(nameSum)

	// Make the "splatter"
	for y := 0; y < AvatarSize; y++ {
		for x := 0; x < AvatarSize; x++ {
			if ((x + y) % 2) == 0 {
				if rand.Intn(2) == 1 {
					avatar.SetRGBA(x, y, pixelColor)
				}
			}
		}
	}

	// Mirror left half to right half
	for y := 0; y < AvatarSize; y++ {
		for x := 0; x < AvatarSize; x++ {
			if x < AvatarSize/2 {
				avatar.Set(AvatarSize-x-1, y, avatar.At(x, y))
			}
		}
	}

	// Mirror top to bottom
	for y := 0; y < AvatarSize; y++ {
		for x := 0; x < AvatarSize; x++ {
			if y < AvatarSize/2 {
				avatar.Set(x, AvatarSize-y-1, avatar.At(x, y))
			}
		}
	}
}

func PaintBG(avatar *image.RGBA, bgColor color.RGBA) {
	for y := 0; y < AvatarSize; y++ {
		for x := 0; x < AvatarSize; x++ {
			avatar.SetRGBA(x, y, bgColor)
		}
	}
}

func CalcPixelColor(nameBytes []byte) (pixelColor color.RGBA) {
	pixelColor.A = 255

	var mutator = byte((len(nameBytes) * 4))

	pixelColor.R = nameBytes[0] * mutator
	pixelColor.G = nameBytes[1] * mutator
	pixelColor.B = nameBytes[2] * mutator

	return
}

func CalcBGColor(nameBytes []byte) (bgColor color.RGBA) {
	bgColor.A = 255

	var mutator = byte((len(nameBytes) * 2))

	bgColor.R = nameBytes[0] * mutator
	bgColor.G = nameBytes[1] * mutator
	bgColor.B = nameBytes[2] * mutator

	return
}

More

1

See Also


Discussion

Login Topics