Skip to content

eliquious/gift

 
 

Repository files navigation

GO IMAGE FILTERING TOOLKIT (GIFT)

Package gift provides a set of useful image processing filters.

Pure Go. No external dependencies outside of the Go standard library.

INSTALLATION / UPDATING

go get -u github.com/disintegration/gift

DOCUMENTATION

http://godoc.org/github.com/disintegration/gift

QUICK START

// 1. Create a new GIFT and add some filters:
g := gift.New(
    gift.Resize(800, 0, gift.LanczosResampling),
    gift.UnsharpMask(1.0, 1.0, 0.0),
)

// 2. Create a new image of the corresponding size.
// dst is a new target image, src is the original image
dst := image.NewRGBA(g.Bounds(src.Bounds()))

// 3. Use Draw func to apply the filters to src and store the result in dst:
g.Draw(dst, src)

USAGE

To create a sequence of filters, the New function is used:

g := gift.New(
   gift.Grayscale(),
   gift.Contrast(10),
)

Filters also can be added using the Add method:

g.Add(GaussianBlur(2)) 

The Bounds method takes the bounds of the source image and returns appropriate bounds for the destination image to fit the result (for example, after using Resize or Rotate filters).

dst := image.NewRGBA(g.Bounds(src.Bounds()))

There are two methods available to apply these filters to an image:

  • Draw applies all the added filters to the src image and outputs the result to the dst image starting from the top-left corner (Min point).
g.Draw(dst, src)
  • DrawAt provides more control. It outputs the filtered src image to the dst image at the specified position using the specified image composition operator. This example is equivalent to the previous:
g.DrawAt(dst, src, dst.Bounds().Min, gift.CopyOperator)

Two image composition operators are supported by now:

  • CopyOperator - Replaces pixels of the dst image with pixels of the filtered src image. This mode is used by the Draw method.
  • OverOperator - Places the filtered src image on top of the dst image. This mode makes sence if the filtered src image has transparent areas.

Empty filter list can be used to create a copy of an image or to paste one image to another. For example:

// Create a new image with dimensions of bgImage
dstImage := image.NewNRGBA(bgImage.Bounds())
// Copy the bgImage to the dstImage.
gift.New().Draw(dstImage, bgImage)
// Draw the fgImage over the dstImage at the (100, 100) position.
gift.New().DrawAt(dstImage, fgImage, image.Pt(100, 100), gift.OverOperator)

SUPPORTED FILTERS

  • Transformations

    • Crop(rect image.Rectangle)
    • CropToSize(width, height int, anchor Anchor)
    • FlipHorizontal()
    • FlipVertical()
    • Resize(width, height int, resampling Resampling)
    • ResizeToFill(width, height int, resampling Resampling, anchor Anchor)
    • ResizeToFit(width, height int, resampling Resampling)
    • Rotate(angle float32, backgroundColor color.Color, interpolation Interpolation)
    • Rotate180()
    • Rotate270()
    • Rotate90()
    • Transpose()
    • Transverse()
  • Adjustments & effects

    • Brightness(percentage float32)
    • ColorBalance(percentageRed, percentageGreen, percentageBlue float32)
    • ColorFunc(fn func(r0, g0, b0, a0 float32) (r, g, b, a float32))
    • Colorize(hue, saturation, percentage float32)
    • ColorspaceLinearToSRGB()
    • ColorspaceSRGBToLinear()
    • Contrast(percentage float32)
    • Convolution(kernel []float32, normalize, alpha, abs bool, delta float32)
    • Gamma(gamma float32)
    • GaussianBlur(sigma float32)
    • Grayscale()
    • Hue(shift float32)
    • Invert()
    • Maximum(ksize int, disk bool)
    • Mean(ksize int, disk bool)
    • Median(ksize int, disk bool)
    • Minimum(ksize int, disk bool)
    • Saturation(percentage float32)
    • Sepia(percentage float32)
    • Sigmoid(midpoint, factor float32)
    • Sobel()
    • UnsharpMask(sigma, amount, thresold float32)

FILTER EXAMPLES

Resize using lanczos resampling
gift.Resize(200, 0, gift.LanczosResampling)
Original image Filtered image
original filtered
Resize using linear resampling
gift.Resize(200, 0, gift.LinearResampling)
Original image Filtered image
original filtered
Resize to fit 160x160px bounding box
gift.ResizeToFit(160, 160, gift.LanczosResampling)
Original image Filtered image
original filtered
Resize to fill 160x160px rectangle, anchor: center
gift.ResizeToFill(160, 160, gift.LanczosResampling, gift.CenterAnchor)
Original image Filtered image
original filtered
Crop 90, 90 - 250, 250
gift.Crop(image.Rect(90, 90, 250, 250))
Original image Filtered image
original filtered
Crop to size 160x160px, anchor: center
gift.CropToSize(160, 160, gift.CenterAnchor)
Original image Filtered image
original filtered
Rotate 90 degrees
gift.Rotate90()
Original image Filtered image
original filtered
Rotate 180 degrees
gift.Rotate180()
Original image Filtered image
original filtered
Rotate 270 degrees
gift.Rotate270()
Original image Filtered image
original filtered
Rotate 30 degrees, white background, cubic interpolation
gift.Rotate(30, color.White, gift.CubicInterpolation)
Original image Filtered image
original filtered
Flip horizontal
gift.FlipHorizontal()
Original image Filtered image
original filtered
Flip vertical
gift.FlipVertical()
Original image Filtered image
original filtered
Transpose
gift.Transpose()
Original image Filtered image
original filtered
Transverse
gift.Transverse()
Original image Filtered image
original filtered
Contrast +30%
gift.Contrast(30)
Original image Filtered image
original filtered
Contrast -30%
gift.Contrast(-30)
Original image Filtered image
original filtered
Brightness +30%
gift.Brightness(30)
Original image Filtered image
original filtered
Brightness -30%
gift.Brightness(-30)
Original image Filtered image
original filtered
Saturation +50%
gift.Saturation(50)
Original image Filtered image
original filtered
Saturation -50%
gift.Saturation(-50)
Original image Filtered image
original filtered
Hue +45
gift.Hue(45)
Original image Filtered image
original filtered
Hue -45
gift.Hue(-45)
Original image Filtered image
original filtered
Sigmoid 0.5, 5.0
gift.Sigmoid(0.5, 5.0)
Original image Filtered image
original filtered
Gamma correction = 0.5
gift.Gamma(0.5)
Original image Filtered image
original filtered
Gamma correction = 1.5
gift.Gamma(1.5)
Original image Filtered image
original filtered
Gaussian blur, sigma=1.0
gift.GaussianBlur(1.0)
Original image Filtered image
original filtered
Unsharp mask, sigma=1.0, amount=1.5, thresold=0.0
gift.UnsharpMask(1.0, 1.5, 0.0)
Original image Filtered image
original filtered
Grayscale
gift.Grayscale()
Original image Filtered image
original filtered
Sepia, 100%
gift.Sepia(100)
Original image Filtered image
original filtered
Invert
gift.Invert()
Original image Filtered image
original filtered
Colorize, blue, saturation=50%
gift.Colorize(240, 50, 100)
Original image Filtered image
original filtered
Color balance, +20% red, -20% green
gift.ColorBalance(20, -20, 0)
Original image Filtered image
original filtered
Color function
gift.ColorFunc(
    func(r0, g0, b0, a0 float32) (r, g, b, a float32) {
        r = 1 - r0   // invert the red channel
        g = g0 + 0.1 // shift the green channel by 0.1
        b = 0        // set the blue channel to 0
        a = a0       // preserve the alpha channel
        return
    },
)
Original image Filtered image
original filtered
Local mean, disc shape, size=5
gift.Mean(5, true)
Original image Filtered image
original filtered
Local median, disc shape, size=5
gift.Median(5, true)
Original image Filtered image
original filtered
Local minimum, disc shape, size=5
gift.Minimum(5, true)
Original image Filtered image
original filtered
Local maximum, disc shape, size=5
gift.Maximum(5, true)
Original image Filtered image
original filtered
Convolution matrix - Emboss
gift.Convolution(
    []float32{
        -1, -1, 0,
        -1, 1, 1,
        0, 1, 1,
    },
    false, false, false, 0.0,
)
Original image Filtered image
original filtered
Convolution matrix - Edge detection
gift.Convolution(
    []float32{
        -1, -1, -1,
        -1, 8, -1,
        -1, -1, -1,
    },
    false, false, false, 0.0,
)
Original image Filtered image
original filtered
Sobel operator
gift.Sobel()
Original image Filtered image
original filtered

About

Go Image Filtering Toolkit

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%