liveBook · Manning

Image package

As explained in Learn Go with Pocket-Sized Projects

The image package is Go's standard-library package for representing, decoding, encoding, and manipulating images.

Go’s image package represents, decodes, encodes, and manipulates raster images: two-dimensional grids whose pixels carry colors or other numerical information. It fits projects such as a maze solver, where exact pixel values encode walls, paths, an entrance, and treasure. section 9.1.1 section 9.7

Use the package to work through an image:

  • Decode a file with the format-specific package, such as image/png, into an image.Image. section 9.7
  • Read pixels through Image.At; use RGBAAt when the concrete image is image.RGBA and direct color comparisons are useful. section 9.1.1 section 9.7
  • Write pixels with RGBA.Set, scanning rows outside columns so the nested loops follow the image’s scanlines. section 9.7
  • Encode with the package for the chosen format; PNG preserves exact values, while JPEG may alter pixel colors through lossy compression. section 9.1.1 section 9.7

Encoding a maze grid

Represent the maze as a raster image, assign colors to walls, paths, the entrance, and treasure, then write the result as PNG so decoding preserves those exact cell values. section 9.1.1

> go doc image
package image // import "image"

[...]

type Image interface{ ... }
    func Decode(r io.Reader) (Image, string, error)

type Point struct{ ... }

type RGBA struct{ ... }
    func NewRGBA(r Rectangle) *RGBA

> go doc image.Point
type Point struct {
    X, Y int
}
    A Point is an X, Y coordinate pair. The axes increase right and down.

The Go documentation listing provides quick access to Image.Decode, Point coordinates, RGBA, and NewRGBA; Point axes increase rightward and downward. section 9.1.1

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains decoding, encoding, pixel access, mutation, coordinate orientation, and scan order without detailing format internals.
Examples
Uses maze walls, paths, entrance, treasure, heat maps, MRI density, flowers, and country palettes as pixel-value examples.
What you can do after
Apply it via image/png, Image.At, RGBAAt, Set, and nested loops to inspect, create, modify, and encode raster images.
How it connects
Connects raster images with image.Image, RGBA, NRGBA, color models, PNG, JPEG, and GIF.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.