Working with Images in Go (Golang): Library Overview and Practical Examples
Go (Golang) is a compiled, statically typed language that is gaining popularity in web development, microservices, and data processing tasks. Unlike Python, where OpenCV or Pillow are traditionally used for image processing, Go has several lightweight and fast libraries that allow you to perform basic raster graphics operations: resizing, cropping, rotating, applying filters, and drawing.
In this article, we will look at three of the most popular libraries for working with images in Go: imaging, nfnt/resize, and gg. You will learn how to install them and see code examples for typical tasks.
1. The imaging Library
imaging is a simple but powerful library for image processing. It provides convenient functions for resizing, cropping, rotating, applying filters (blur, sharpen, color inversion), and saving in various formats (JPEG, PNG, GIF, BMP).
Installation
go get -u github.com/disintegration/imagingExample: Resizing and Cropping
package main
import ( "fmt" "github.com/disintegration/imaging" "log")
func main() { // Open the image src, err := imaging.Open("input.jpg") if err != nil { log.Fatalf("Error opening file: %v", err) }
// Resize to 800x600, maintaining aspect ratio dst := imaging.Resize(src, 800, 600, imaging.Lanczos) // Crop the center area 400x400 dst = imaging.CropCenter(dst, 400, 400)
// Apply a blur filter dst = imaging.Blur(dst, 3.0)
// Save the result err = imaging.Save(dst, "output.jpg") if err != nil { log.Fatalf("Error saving: %v", err) } fmt.Println("Image processed and saved as output.jpg")}The library supports many filters: imaging.Grayscale, imaging.Invert, imaging.Contrast, imaging.Sharpen, and others. They all work "out of the box".
2. The nfnt/resize Library
If your task is only to resize images with maximum performance, nfnt/resize is the best choice. It is optimized for high-speed processing and supports various interpolation algorithms (NearestNeighbor, Bilinear, Bicubic, Lanczos2, Lanczos3).
Installation
go get -u github.com/nfnt/resizeExample: Resizing with Different Algorithms
package main
import ( "image" "image/jpeg" "log" "os" "github.com/nfnt/resize")
func main() { file, err := os.Open("input.jpg") if err != nil { log.Fatal(err) } defer file.Close()
img, _, err := image.Decode(file) if err != nil { log.Fatal(err) }
// Resize to width 300 pixels, height is calculated automatically m := resize.Resize(300, 0, img, resize.Lanczos3)
out, err := os.Create("resized.jpg") if err != nil { log.Fatal(err) } defer out.Close()
// Save with JPEG quality 90% err = jpeg.Encode(out, m, &jpeg.Options{Quality: 90}) if err != nil { log.Fatal(err) } log.Println("Image resized to width 300px")}This library does not provide functions for cropping or filters, but its speed and minimalism make it ideal for servers that need to process thousands of images per second.
3. The gg Library (drawing)
gg is a library for drawing 2D graphics, similar to cairo or Pillow Draw. It allows you to create images