Introduction to Working with Images in Go
Go (Golang) is a modern compiled language with a rich standard library that includes the image package for basic work with raster images. However, tasks such as scaling, filtering, format conversion, and batch processing often require third-party libraries. In this article, we will review the most popular tools for working with images in Go, provide code examples, and compare their capabilities.
Standard image Package
The image package from the Go standard library provides basic types and functions for decoding, encoding, and manipulating images. It supports PNG, JPEG, and GIF formats (decoding only). Example of loading an image from a file:
package main
import ( "image" "image/jpeg" "os")
func main() { file, err := os.Open("input.jpg") if err != nil { panic(err) } defer file.Close()
img, _, err := image.Decode(file) if err != nil { panic(err) }
// Access pixels via img.At(x, y) _ = img}Limitations of the standard package: no built-in functions for resizing, rotation, filtering, or working with EXIF data. Use third-party libraries for these tasks.
imaging Library
imaging is one of the most popular libraries for image processing in Go. It provides a simple API for resizing, cropping, rotating, applying filters, and format conversion.
Installation:
go get github.com/disintegration/imagingExample of resizing while maintaining aspect ratio:
package main
import ( "github.com/disintegration/imaging" "log")
func main() { src, err := imaging.Open("input.jpg") if err != nil { log.Fatalf("Error opening: %v", err) }
// Resize to width 800px, height adjusts automatically dst := imaging.Resize(src, 800, 0, imaging.Lanczos)
err = imaging.Save(dst, "output.jpg") if err != nil { log.Fatalf("Error saving: %v", err) }}The library supports filters: blur, sharpen, contrast, brightness, grayscale. Example of applying a filter:
grayscaleImg := imaging.Grayscale(src)sharpImg := imaging.Sharpen(src, 0.5)blurImg := imaging.Blur(src, 3.0)Advantages: clean API, GIF support (read-only), automatic EXIF orientation, built-in functions for thumbnails and batch processing.
nfnt/resize Library
nfnt/resize is a specialized library for resizing images. It offers several interpolation algorithms (NearestNeighbor, Bilinear, Bicubic, MitchellNetravali, Lanczos2, Lanczos3).
Installation:
go get github.com/nfnt/resizeExample of resizing to exactly 200x200 pixels:
package main
import ( "image/jpeg" "os" "github.com/nfnt/resize")
func main() { file, _ := os.Open("input.jpg") defer file.Close() img, _ := jpeg.Decode(file)
// Resize to 200x200 newImg := resize.Resize(200, 200, img, resize.Lanczos3)
out, _ := os.Create("resized.jpg") defer out.Close() jpeg.Encode(out, newImg, nil)}Features: the library only works with images implementing the image.Image interface. It does not support direct file reading/writing — manual decoding/encoding is required.
Also in library
Image Processing in Rust: resize and crop with image and imageproc
Working with CSV in Java: OpenCSV, Apache Commons CSV, and Java Scanner Libraries
Working with CSV in Kotlin: Libraries and Code Examples
MPAndroidChart for Kotlin: Complete Guide with Examples