Skip to content

Effects ​

Introduction ​

Effects change every frame of the image in place and return the image. Levels outside a method's range don't panic. They set the image's error, which Encode, Save, Store, and image.Response return:

go
img.Blur(500) // the blur level 500 is outside 0 to 100

if err := img.Err(); err != nil {
	// ...
}

Adjusting colors ​

MethodRangeEffect
Brightness(level)-100 to 100Darkens or lightens the image. -100 turns it black and 100 white.
Contrast(level)-100 to 100Lowers or raises the difference between light and dark. -100 turns it an even gray.
Gamma(gamma)above 0Values above 1 lighten midtones, and values below 1 darken them.
Colorize(red, green, blue)-100 to 100 eachShifts each color channel by a percentage.
Grayscale()Turns colors into shades of gray by their luminance.
Invert()Replaces colors with their opposites and keeps transparency.
go
img.Grayscale().Contrast(15).Brightness(5)

Filters ​

MethodRangeEffect
Blur(level)0 to 100Applies a Gaussian blur level times.
Sharpen(level)0 to 100Sharpens edges.
Pixelate(size)1 or moreReplaces each size by size block with its average color.
go
img.Pixelate(12)

Reducing colors ​

ReduceColors limits each frame to at most a number of colors, from 1 to 256, and keeps transparency. Frames with fewer colors stay as they are:

go
img.ReduceColors(64)

The result is the same for the same image every time, so cached files don't change between requests.

Filling ​

Fill paints the whole image with a color. A translucent color blends over the image:

go
img.Fill("rgba(0, 0, 0, 0.4)")

FillAt fills the area around a pixel that shares its color, like a paint bucket. FillTransparentAreas replaces transparent and translucent pixels with the image's background color:

go
img.FillAt("#ff2d20", 10, 10)
img.SetBackground("#fff").FillTransparentAreas()

Copying before you change an image ​

Because modifiers change the image in place, make a copy with Clone when you need several versions of one image:

go
original, err := image.From(r).Open("photo.jpg")
if err != nil {
	return err
}

thumbnail := original.Clone().Cover(300, 300, image.Center)
preview := original.Clone().Scale(1200, 0).Blur(10)

Clone copies the frames, Exif data, background color, and error, so the copies change independently.

Released under the MIT License.