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:
img.Blur(500) // the blur level 500 is outside 0 to 100
if err := img.Err(); err != nil {
// ...
}Adjusting colors
| Method | Range | Effect |
|---|---|---|
Brightness(level) | -100 to 100 | Darkens or lightens the image. -100 turns it black and 100 white. |
Contrast(level) | -100 to 100 | Lowers or raises the difference between light and dark. -100 turns it an even gray. |
Gamma(gamma) | above 0 | Values above 1 lighten midtones, and values below 1 darken them. |
Colorize(red, green, blue) | -100 to 100 each | Shifts 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. |
img.Grayscale().Contrast(15).Brightness(5)Filters
| Method | Range | Effect |
|---|---|---|
Blur(level) | 0 to 100 | Applies a Gaussian blur level times. |
Sharpen(level) | 0 to 100 | Sharpens edges. |
Pixelate(size) | 1 or more | Replaces each size by size block with its average color. |
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:
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:
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:
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:
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.