Image Processing in Go: Resizing and Cropping (resize, crop)

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Images in Go: Resizing (resize) and Cropping (crop)



In modern web development and backend service creation, there is often a need to process graphic files: reducing file size, converting to a uniform format, and creating thumbnails. The Go language (Golang) offers several powerful and performant libraries for working with images. In this article, we will take a detailed look at two key operations — resizing (resize) and cropping (crop) — using popular tools.



Why Go for Image Processing?

Go compiles to native code, has high performance, and built-in support for multithreading. This makes it an excellent choice for creating high-load image processing services. Unlike Python, which often uses the Pillow library (C extensions), Go provides pure Go implementations that integrate easily into a microservice architecture.



Main Libraries

  • github.com/disintegration/imaging — the most popular library. Provides a convenient API for resize, crop, rotate, blur, and other operations. Works on top of the standard image package.
  • github.com/nfnt/resize — one of the first libraries. Known for its high speed and support for various interpolation algorithms (NearestNeighbor, Bilinear, Lanczos2, Lanczos3).
  • github.com/anthonynsimon/bild — a newer project focused on performance and code cleanliness. Supports resize, crop, and effects.


In this article, we will focus on the imaging library as the most balanced in terms of functionality and ease of use.



Installation

To get started, you need to install the library using go get:

go get github.com/disintegration/imaging


Resizing an Image (Resize)

The resize operation allows you to scale an image to new dimensions. The imaging library supports several modes:

  • Fit — fits the image into a given rectangle, preserving the aspect ratio.
  • Fill — fills the rectangle, cropping the edges if necessary.
  • Resize — exact resizing to the specified width and height (without preserving the aspect ratio).


Example of basic resizing while preserving the aspect ratio (Fit):

package main

import ( "github.com/disintegration/imaging" "log")

func main() { // Open the source image src, err := imaging.Open("input.jpg") if err != nil { log.Fatalf("Error opening: %v", err) }

// Resize to a width of 800px, height is calculated automatically dst := imaging.Fit(src, 800, 600, imaging.Lanczos)

// Save the result err = imaging.Save(dst, "output_fit.jpg") if err != nil { log.Fatalf("Error saving: %v", err) } log.Println("Image processed successfully!")}


If you need exact resizing (e.g., to create a 150x150 preview):

// Exact resize (may distort the aspect ratio)dst := imaging.Resize(src, 150, 150, imaging.Lanczos)


Cropping an Image (Crop)

Cropping allows you to remove a part of the image. In imaging, there are two main methods:

  • Crop — cuts out a rectangular area based on given coordinates.
  • CropCenter — cuts out the central area of a given size.


Example of cropping the central part (useful for creating square avatars):

package main

import ( "github.com/disintegration/imaging" "log")

func main() { src, err :

Recommendations