Go's standard library handles 2D raster images well through the image package family: you can create, draw on, decode and encode PNG, JPEG and GIF files. It has no audio, video or GUI support, and no hardware-accelerated graphics, so for those you use third-party libraries such as Ebitengine, Fyne or golang.org/x/image.
The sections below cover what the standard library does, drawing and compositing, font rendering, animated GIFs, and when to reach for outside packages.
| Package | Purpose |
|---|---|
image | Core types: Image, RGBA, NRGBA, Gray, Rectangle, Point, plus Decode |
image/color | Color models such as color.RGBA, color.NRGBA and color.Gray |
image/draw | Compositing, copying and filling images |
image/png, image/jpeg, image/gif | Encoders and decoders (importing registers the format for image.Decode) |
image/color/palette | Standard palettes for GIFs |
The golang.org/x/image module extends this with WebP and BMP decoding, TIFF, font rendering (font, font/basicfont, font/opentype) and scaling (draw with interpolators).
Create an *image.RGBA, fill it with draw.Draw, and encode it. The original examples ignored errors, but file creation and encoding both return errors that you should handle.
draw.Src replaces the destination pixels, while draw.Over blends the source over what is already there, which matters when the source has transparency.
The standard library has no line or circle function, but drawing is simple: set pixels with Set, or fill rectangles by drawing a uniform color into a sub-rectangle.
For anti-aliased lines, circles, paths and text, use a drawing library such as github.com/fogleman/gg or github.com/llgcode/draw2d.
image.Decode detects the format by looking at the file header, as long as you import the matching decoder for its side effect with a blank import.
Check img.Bounds() before indexing pixels, because an image's origin is not always (0, 0).
The image/gif package can write animations with gif.EncodeAll. Each frame is a paletted image.
| Goal | Library |
|---|---|
| Resize, blur and sharpen | github.com/disintegration/imaging or golang.org/x/image/draw |
| Vector drawing and text | github.com/fogleman/gg |
| 2D games | github.com/hajimehoshi/ebiten/v2 (Ebitengine) |
| Desktop GUI | fyne.io/fyne/v2 |
| Charts | gonum.org/v1/plot |
| GPU graphics | OpenGL or Vulkan bindings, or Ebitengine and Gio |
The standard library has no audio or video support. Use external codecs or call tools such as FFmpeg from a separate process when you need them.
runtime.NumCPU() goroutines, because decoding and resizing are CPU-bound.image.DecodeConfig to read the dimensions cheaply and reject huge images.*image.NRGBA when working with non-premultiplied PNG data and *image.RGBA for compositing.defer and handle encoder errors, since a failed write can leave a corrupt file.Yes, with golang.org/x/image/font and a face from basicfont or opentype, or with a library such as gg that wraps this.
Decoding is available through golang.org/x/image/webp. Encoding needs a third-party package.
Use golang.org/x/image/draw with draw.CatmullRom.Scale, or the imaging package for a simpler API.
Not with the standard library. Use Fyne, Gio or Ebitengine.
Go's image packages cover creating, drawing, decoding and encoding PNG, JPEG and GIF images. For text, scaling, vector graphics, games and GUIs, add golang.org/x/image or a third-party library, and keep untrusted image handling bounded and error-checked.