Working with Images in Go (Golang): image, draw, imaging, resize

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 (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 Image interface 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

Recommendations