Go's standard library can create, decode, transform and encode raster images with image, image/draw, image/color, image/png, image/jpeg and image/gif, and it can serve media and stream bytes over HTTP. It cannot decode or encode audio and video codecs, render vector graphics or play sound, so for those you use golang.org/x/image, an external library or an ffmpeg process.
This article shows practical image work, serving media, and where the standard library stops.
| Package | Purpose |
|---|---|
image | The Image interface and concrete types such as RGBA, NRGBA, Gray, plus Rect and Point |
image/color | Color models and conversions |
image/draw | Compositing: fill, copy and blend images |
image/png, image/jpeg, image/gif | Encoders and decoders, registered for image.Decode |
golang.org/x/image (separate module) | WebP and TIFF decoding, resizing, fonts, vector drawing |
The standard library has no WebP or AVIF support and no image resize function. Resizing uses golang.org/x/image/draw.
This program fills a canvas, draws a rectangle with image/draw and writes a PNG. It checks every error, which the original version ignored.
draw.Src copies pixels, while draw.Over blends using alpha. Drawing text or lines needs golang.org/x/image/font and a third-party vector library, because the standard library has neither.
Blank-import the decoders you need so image.Decode can detect the format. The function below converts any supported image to grayscale JPEG.
Per-pixel At and Set are simple but slow on large images. For speed, type-assert to *image.RGBA and work on the Pix slice directly. Check Bounds().Dx() and Dy() before decoding untrusted uploads, or use image.DecodeConfig first to reject huge dimensions.
The image/gif package can write animations from a list of paletted frames.
http.ServeFile and http.ServeContent handle Range requests, If-Modified-Since and content types, which is what video and audio players need for seeking.
http.FileServer is safe against path traversal for the directory you give it, but it lists directories by default, so serve a dedicated media folder. The standard library has no WebSocket server and no HLS or DASH packager. Produce those segments with ffmpeg and serve the files as static content.
The standard library has no audio or video codecs. encoding/binary is enough to write a simple WAV header and PCM samples yourself, and os/exec can call ffmpeg for transcoding. For playback or capture you need a third-party library that wraps the platform audio API.
The standard library cannot. Use golang.org/x/image/draw with a scaler such as draw.CatmullRom, or a library built on it.
Only decoding, through golang.org/x/image/webp. For encoding use a third-party package or an external tool.
Yes, with golang.org/x/image/font and a font face, but not with the standard library alone.
Reasonably, especially across goroutines on separate images or tiles. Avoid per-pixel interface calls in hot loops and operate on Pix directly.
Use image, image/draw, image/color and the PNG, JPEG and GIF packages for raster work, and net/http to serve media with range support. Validate dimensions of untrusted images, check every error, and add golang.org/x/image, third-party codecs or ffmpeg for resizing, text, WebP, audio and video.