Working with Images in Go (Golang): From Basics to Advanced Tools
Go (Golang) provides powerful built-in tools for working with raster graphics, as well as a rich ecosystem of third-party libraries. In this article, we will cover both the standard packages image, image/color, image/draw, and image/png, as well as popular libraries imaging, resize, and gg for solving common tasks: loading, resizing, overlaying, drawing, and saving images.
Standard Go Packages for Working with Images
The Go standard library includes basic types and functions for working with images that cover 80% of everyday tasks.
- image — defines the
Imageinterface and concrete types:RGBA,NRGBA,Gray,Paletted, and others. - image/color — describes colors and color models (
Color,RGBA,NRGBA,Gray). - image/draw — provides drawing and image compositing algorithms (Porter-Duff compositing).
- image/png, image/jpeg, image/gif — codecs for reading/writing the respective formats.
Example 1: Creating and Saving a PNG with a Gradient
package main
import ( "image" "image/color" "image/png" "os")
func main() { // Create a new RGBA image 200x200 width, height := 200, 200 img := image.NewRGBA(image.Rect(0, 0, width, height))
// Fill pixels with a gradient from red to blue for y := 0; y < height; y++ { for x := 0; x < width; x++ { // Ratio from 0 to 1 ratio := float64(x) / float64(width) r := uint8(255 * (1 - ratio)) b := uint8(255 * ratio) g := uint8(128) img.Set(x, y, color.RGBA{r, g, b, 255}) } }
// Save to file f, _ := os.Create("gradient.png") defer f.Close() png.Encode(f, img)}Example 2: Loading JPEG, Overlaying Text (via image/draw)
package main
import ( "image" "image/color" "image/draw" "image/jpeg" "image/png" "os")
func main() { // Open the source image srcFile, _ := os.Open("input.jpg") defer srcFile.Close() src, _, _ := image.Decode(srcFile)
// Create an RGBA copy for drawing bounds := src.Bounds() dst := image.NewRGBA(bounds) draw.Draw(dst, bounds, src, bounds.Min, draw.Src)
// Draw a red rectangle in the top-left corner redRect := image.NewUniform(color.RGBA{255, 0, 0, 200}) rect := image.Rect(10, 10, 150, 80) draw.Draw(dst, rect, redRect, image.Point{}, draw.Over)
// Save the result outFile, _ := os.Create("output.png") defer outFile.Close() png.Encode(outFile, dst)}Imaging Library: High-Level Processing
imaging (github.com/disintegration/imaging) is a popular library that provides a convenient API for the most common operations: resizing, cropping, rotating, applying filters, and conversion.
imaging.Open()— load an image from a file.imaging.Resize()— resize while maintaining aspect ratio.imaging.Fit()— fit into a given rectangle.imaging.Fill()— crop and scale to an exact size.imaging.Rotate()— rotate by a given angle.imaging.Blur(),imaging.Sharpen()— filters.
Example 3: Resizing with
Also in library
ts-jest for TypeScript: Complete Guide with Examples
Monitoring PHP Applications with PHPMetrics
Redis client in Rust: complete guide to working with the redis-rs library
Task Scheduler in Go (Golang): cron, gocron and robfig/cron