image

Guided tour · Image · pkg.go.dev →

Core image types: Image interface, Rectangle, Point, and concrete types like RGBA / Gray / NRGBA.

Create an image

NewRGBA

img := image.NewRGBA(image.Rect(0, 0, 200, 100))
img.Set(10, 10, color.RGBA{R: 255, A: 255})

NewGray / NewNRGBA

gray := image.NewGray(image.Rect(0, 0, 64, 64))
nrgba := image.NewNRGBA(image.Rect(0, 0, 64, 64))

Decode (format-agnostic)

Import the format packages for their side effects to auto-register decoders.

Decode any registered format

import (
    _ "image/png"
    _ "image/jpeg"
    _ "image/gif"
)

f, _ := os.Open("input")
defer f.Close()
img, format, err := image.Decode(f)
fmt.Println(format) // "png" / "jpeg" / "gif"

DecodeConfig (no pixels)

cfg, _, _ := image.DecodeConfig(f)
fmt.Println(cfg.Width, cfg.Height)

Geometry

Rectangle / Point

r := image.Rect(0, 0, 100, 50)
r.Dx()      // width
r.Dy()      // height
r.Intersect(image.Rect(50, 0, 150, 100))