Working with Images in Go: Resize and Crop with the imaging Library

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

The imaging Library for Go: Resize and Crop Images



In modern web development, image processing is one of the key tasks: from creating thumbnails to adapting images for different screen resolutions. If you are writing in Go (Golang) and looking for a simple yet powerful tool for working with images, check out the imaging library. It provides a convenient API for resizing, cropping, rotating, filtering, and many other operations.



What is imaging?



imaging is a popular library for Go, built on top of the image package from the standard library, but with a much more user-friendly interface. It supports JPEG, PNG, GIF, and BMP formats, and allows you to perform operations with minimal code. Its main advantages are speed and ease of integration.



Installation



To install the library, run the following in your terminal:



go get -u github.com/disintegration/imaging


After that, you can import it into your project:



import "github.com/disintegration/imaging"


Basic Operations: Resize and Crop



1. Resizing



The imaging.Resize function allows you to change the image size to the specified width and height. To maintain the aspect ratio, pass 0 for one of the dimensions — the library will automatically calculate the other.



Example: Resize to 300 pixels wide



package main

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

func main() { // Open the image img, err := imaging.Open("input.jpg") if err != nil { panic(err) }

// Set width to 300px, height adjusts automatically resized := imaging.Resize(img, 300, 0, imaging.Lanczos)

// Save the result err = imaging.Save(resized, "output_resized.jpg") if err != nil { panic(err) }}


The example uses the imaging.Lanczos filter — one of the highest quality for resizing. You can also choose others: NearestNeighbor, Box, Linear, Gaussian, etc.



2. Cropping



The imaging.Crop function cuts out a rectangular area of the image. You need to pass the coordinates of the top-left corner, as well as the width and height of the area.



Example: Crop a central 200x200 pixel area



package main

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

func main() { img, err := imaging.Open("input.jpg") if err != nil { panic(err) }

// Calculate coordinates for central cropping width := img.Bounds().Dx() height := img.Bounds().Dy() x := (width - 200) / 2 y := (height - 200) / 2

// Crop cropped := imaging.Crop(img, imaging.Rect(x, y, x+200, y+200))

err = imaging.Save(cropped, "output_cropped.jpg") if err != nil { panic(err) }}


If you simply need to crop to a square from the center, use imaging.CropCenter:



croppedCenter := imaging.CropCenter(img, 200, 200)


Advanced Features



In addition to basic operations, imaging offers:



  • Thumbnail — creating a thumbnail while preserving aspect ratio and filling the background.
  • Rotate — rotation by an arbitrary angle.
  • Flip — horizontal or vertical mirroring.
  • Blur — image blurring.
  • Sharpen — increasing sharpness.
  • Grayscale — conversion to black and white.


Example: Creating a 150x150 thumbnail

Recommendations