bild

package module
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2016 License: MIT Imports: 13 Imported by: 1

README

bild

bild logo

MIT License GoDoc Build Status Go Report Card

import "github.com/anthonynsimon/bild"

Simple image processing in Go with parallel processing support.

Package bild provides a collection of common image processing functions. The input images must implement the image.Image interface and the functions return an *image.RGBA.

The aim of this project is simplicity in use and development over high performance, but most algorithms are designed to be efficient and make use of parallelism when available. It is based on standard Go packages to reduce dependency use and development abstractions.

Notice: This package is under heavy development and the API might change at any time until a 1.0 version is reached.

Install

bild requires Go version 1.4 or greater.

go get -u github.com/anthonynsimon/bild

Documentation

http://godoc.org/github.com/anthonynsimon/bild

Basic example:

package main

import "github.com/anthonynsimon/bild"

func main() {
	img, err := bild.Open("filename")
	if err != nil {
		panic(err)
	}

	result := bild.Invert(img)

	if err := bild.Save("filename", result, bild.PNG); err != nil {
		panic(err)
	}
}

Output examples

Adjustment

Brightness
result := bild.Brightness(img, 0.25)

example

Contrast
result := bild.Contrast(img, -0.5)

example

Gamma
result := bild.Gamma(img, 2.2)

example

Blend modes

result := bild.Multiply(bg, fg)
Add Color Burn Color Dodge
Darken Difference Divide
Exclusion Lighten Linear Burn
Linear Light Multiply Normal Blend
Opacity Overlay Screen
Soft Light Subtract

Blur

BoxBlur
result := bild.BoxBlur(img, 3.0)

example

GaussianBlur
result := bild.GaussianBlur(img, 3.0)

example

Channel

ExtractChannel
result := bild.ExtractChannel(img, bild.Alpha)

example

Effects

EdgeDetection
result := bild.EdgeDetection(img, 1.0)

example

Emboss
result := bild.Emboss(img)

example

Grayscale
result := bild.Grayscale(img)

example

Invert
result := bild.Invert(img)

example

Median
result := bild.Median(img, 10.0)

example

Sharpen
result := bild.Sharpen(img)

example

Sobel
result := bild.Sobel(img)

example

Histogram

RGBA Histogram
hist := bild.NewRGBAHistogram(img)
result := hist.Image()

example

Resize

Crop
// Source image is 280x280
result := bild.Crop(img, image.Rect(70,70,210,210))

example

Resize Resampling Filters
result := bild.Resize(img, 280, 280, bild.Linear)
Nearest Neighbor Linear Gaussian
Mitchell Netravali Catmull Rom Lanczos

Segmentation

Threshold
result := bild.Threshold(img, 128)

example

Transform

FlipH
result := bild.FlipH(img)

example

FlipV
result := bild.FlipV(img)

example

Rotate
// Options set to nil will use defaults (ResizeBounds set to false, Pivot at center)
result := bild.Rotate(img, -45.0, nil)

example

// If ResizeBounds is set to true, the full rotation bounding area is used
result := bild.Rotate(img, -45.0, &bild.RotationOptions{ResizeBounds: true})

example

// Pivot coordinates are set from the top-left corner
// Notice ResizeBounds being set to default (false)
result := bild.Rotate(img, -45.0, &bild.RotationOptions{Pivot: &image.Point{0, 0}})

example

License

This project is licensed under the MIT license. Please read the LICENSE file.

Contribute

Want to hack on the project? Any kind of contribution is welcome!
Simply follow the next steps:

  • Fork the project.
  • Create a new branch.
  • Make your changes and write tests when practical.
  • Commit your changes to the new branch.
  • Send a pull request, by the way you are awesome.

Documentation

Overview

Package bild provides a collection of common image processing functions. The input images must implement the image.Image interface and the functions return an *image.RGBA.

The aim of this project is simplicity in use and development over high performance, but most algorithms are designed to be efficient and make use of parallelism when available. It is based on standard Go packages to reduce dependecy use and development abstractions.

Index

Constants

View Source
const (
	Red = iota
	Green
	Blue
	Alpha
)

Channel identifiers

View Source
const (
	JPEG = iota
	PNG
)

Supported image encoding types

Variables

This section is empty.

Functions

func Add

func Add(bg image.Image, fg image.Image) *image.RGBA

Add combines the foreground and background images by adding their values and returns the resulting image.

func BoxBlur

func BoxBlur(src image.Image, radius float64) *image.RGBA

BoxBlur returns a blurred (average) version of the image. Radius must be larger than 0.

func Brightness

func Brightness(src image.Image, change float64) *image.RGBA

Brightness returns a copy of the image with the adjusted brightness. Change is the normalized amount of change to be applied (range -1.0 to 1.0).

func CloneAsRGBA

func CloneAsRGBA(src image.Image) *image.RGBA

CloneAsRGBA returns an RGBA copy of the supplied image.

func ColorBurn

func ColorBurn(bg image.Image, fg image.Image) *image.RGBA

ColorBurn combines the foreground and background images by dividing the inverted background by the foreground image and then inverting the result which is then returned.

func ColorDodge

func ColorDodge(bg image.Image, fg image.Image) *image.RGBA

ColorDodge combines the foreground and background images by dividing background by the inverted foreground image and returns the result.

func Contrast

func Contrast(src image.Image, change float64) *image.RGBA

Contrast returns a copy of the image with its difference in high and low values adjusted by the change param. Change is the normalized amount of change to be applied, in the range of -1.0 to 1.0. If Change is set to 0.0, then the values remain the same, if it's set to 0.5, then all values will be moved 50% away from the middle value.

func Convolve

Convolve applies a convolution matrix (kernel) to an image with the supplied options.

Usage example:

result := Convolve(img, kernel, &ConvolutionOptions{Bias: 0, Wrap: false, CarryAlpha: false})

func Crop

func Crop(img image.Image, rect image.Rectangle) *image.RGBA

Crop returns a new image which contains the intersection between the rect and the image provided as params. Only the intersection is returned. If a rect larger than the image is provided, no fill is done to the 'empty' area.

Usage example:

result := bild.Crop(img, image.Rect(0,0,512,256))

func Darken

func Darken(bg image.Image, fg image.Image) *image.RGBA

Darken combines the foreground and background images by picking the darkest value per channel for each pixel. The result is then returned.

func Difference

func Difference(bg image.Image, fg image.Image) *image.RGBA

Difference calculates the absolute difference between the foreground and background images and returns the resulting image.

func Divide

func Divide(bg image.Image, fg image.Image) *image.RGBA

Divide combines the foreground and background images by diving the values from the background by the foreground and returns the resulting image.

func EdgeDetection

func EdgeDetection(src image.Image, radius float64) *image.RGBA

EdgeDetection returns a copy of the image with it's edges highlighted.

func Emboss

func Emboss(src image.Image) *image.RGBA

Emboss returns a copy of the image in which each pixel has been replaced either by a highlight or a shadow representation.

func Encode

func Encode(w io.Writer, img image.Image, format Format) error

Encode writes an image in the specified format.

Usage example:

// Encode an image to a writer in PNG format,
// returns an error if something went wrong
err := Encode(outFile, img, bild.PNG)

func Exclusion

func Exclusion(bg image.Image, fg image.Image) *image.RGBA

Exclusion combines the foreground and background images applying the Exclusion blend mode and returns the resulting image.

func ExtractChannel

func ExtractChannel(img image.Image, c Channel) *image.Gray

ExtractChannel returns a grayscale image containing the values of the selected channel. Channel indices Red: '0', Green: '1', Blue: '2', Alpha: '3'.

Usage example:

result := bild.ExtractChannel(img, bild.Alpha)

func FlipH

func FlipH(img image.Image) *image.RGBA

FlipH returns a horizontally flipped version of the image.

func FlipV

func FlipV(img image.Image) *image.RGBA

FlipV returns a vertically flipped version of the image.

func Gamma

func Gamma(src image.Image, gamma float64) *image.RGBA

Gamma returns a gamma corrected copy of the image. Provided gamma param must be larger than 0.

func GaussianBlur

func GaussianBlur(src image.Image, radius float64) *image.RGBA

GaussianBlur returns a smoothly blurred version of the image using a Gaussian function. Radius must be larger than 0.

func Grayscale

func Grayscale(img image.Image) *image.Gray

Grayscale returns a copy of the image in Grayscale using the weights 0.3R + 0.6G + 0.1B as a heuristic.

func Invert

func Invert(src image.Image) *image.RGBA

Invert returns a negated version of the image.

func Lighten

func Lighten(bg image.Image, fg image.Image) *image.RGBA

Lighten combines the foreground and background images by picking the brightest value per channel for each pixel. The result is then returned.

func LinearBurn

func LinearBurn(bg image.Image, fg image.Image) *image.RGBA

LinearBurn combines the foreground and background images by adding them and then subtracting 255 (1.0 in normalized scale). The resulting image is then returned.

func LinearLight

func LinearLight(bg image.Image, fg image.Image) *image.RGBA

LinearLight combines the foreground and background images by a mix of a Linear Dodge and Linear Burn operation. The resulting image is then returned.

func Median

func Median(img image.Image, size int) *image.RGBA

Median returns a new image in which each pixel is the median of it's neighbors. Size sets the amount of neighbors to be searched.

func Multiply

func Multiply(bg image.Image, fg image.Image) *image.RGBA

Multiply combines the foreground and background images by multiplying their normalized values and returns the resulting image.

func NormalBlend

func NormalBlend(bg image.Image, fg image.Image) *image.RGBA

NormalBlend combines the foreground and background images by placing the foreground over the background using alpha compositing. The resulting image is then returned.

func Opacity

func Opacity(bg image.Image, fg image.Image, percent float64) *image.RGBA

Opacity returns an image which blends the two input images by the percentage provided. Percent must be of range 0 <= percent <= 1.0

func Open

func Open(filename string) (image.Image, error)

Open loads and decodes an image from a file and returns it.

Usage example:

// Encode an image to a writer in PNG format,
// returns an error if something went wrong
img, err := Open("exampleName")

func Overlay

func Overlay(bg image.Image, fg image.Image) *image.RGBA

Overlay combines the foreground and background images by using Multiply when channel values < 0.5 or using Screen otherwise and returns the resulting image.

func Resize

func Resize(img image.Image, width, height int, filter ResampleFilter) *image.RGBA

Resize returns a new image with its size adjusted to the new width and height. The filter param corresponds to the Resampling Filter to be used when interpolating between the sample points.

Usage example:

result := bild.Resize(img, 800, 600, bild.Linear)

func Rotate

func Rotate(img image.Image, angle float64, options *RotationOptions) *image.RGBA

Rotate returns a rotated image by the provided angle using the pivot as an anchor. Parameters angle is in degrees and it's applied clockwise. Default parameters are used if a nil *RotationOptions is passed.

Usage example:

// Rotate 90.0 degrees clockwise, preserving the image size and the pivot point at the top left corner
result := bild.Rotate(img, 90.0, &bild.RotationOptions{PreserveSize: true, Pivot: &image.Point{0, 0}})

func Save

func Save(filename string, img image.Image, format Format) error

Save creates a file and writes to it an image in the specified format

Usage example:

// Save an image to a file in PNG format,
// returns an error if something went wrong
err := Save("exampleName", img, bild.PNG)

func Screen

func Screen(bg image.Image, fg image.Image) *image.RGBA

Screen combines the foreground and background images by inverting, multiplying and inverting the output. The result is a brighter image which is then returned.

func Sharpen

func Sharpen(src image.Image) *image.RGBA

Sharpen returns a sharpened copy of the image by detecting it's edges and adding it to the original.

func Sobel

func Sobel(src image.Image) *image.RGBA

Sobel returns an image emphasising edges using an approximation to the Sobel–Feldman operator.

func SoftLight

func SoftLight(bg image.Image, fg image.Image) *image.RGBA

SoftLight combines the foreground and background images by using Pegtop's Soft Light formula and returns the resulting image.

func Subtract

func Subtract(bg image.Image, fg image.Image) *image.RGBA

Subtract combines the foreground and background images by Subtracting the background from the foreground. The result is then returned.

func Threshold

func Threshold(img image.Image, level uint8) *image.Gray

Threshold returns a grayscale image in which values from the param img that are smaller than the param level are set to black and values larger than or equal to it are set to white. Level must be of the range 0 to 255.

Types

type Channel

type Channel int

Channel identifier for RGBA images

type ConvolutionMatrix

type ConvolutionMatrix interface {
	At(x, y int) float64
	Normalized() ConvolutionMatrix
	MaxX() int
	MaxY() int
}

ConvolutionMatrix interface. At returns the matrix value at position x, y. Normalized returns a new matrix with normalized values. MaxX returns the horizontal length. MaxY returns the vertical length.

type ConvolutionOptions

type ConvolutionOptions struct {
	Bias       float64
	Wrap       bool
	CarryAlpha bool
}

ConvolutionOptions are the Convolve function parameters. Bias is added to each RGB channel after convoluting. Range is -255 to 255. Wrap sets if indices outside of image dimensions should be taken from the opposite side. CarryAlpha sets if the alpha should be taken from the source image without convoluting

type Format

type Format int

Format is used to identify the image encoding type

type Histogram

type Histogram struct {
	Bins []int
}

Histogram holds a variable length slice of bins, which keeps track of sample counts.

func (*Histogram) Cumulative

func (h *Histogram) Cumulative() *Histogram

Cumulative returns a new Histogram in which each bin is the cumulative value of it's previous bins

func (*Histogram) Image

func (h *Histogram) Image() *image.Gray

Image returns a grayscale image representation of the Histogram. The width and height of the image will be equivalent to the number of Bins in the Histogram.

func (*Histogram) Max

func (h *Histogram) Max() int

Max returns the highest count found in the histogram bins.

func (*Histogram) Min

func (h *Histogram) Min() int

Min returns the lowest count found in the histogram bins.

type Kernel

type Kernel struct {
	Matrix []float64
	Width  int
	Height int
}

Kernel to be used as a convolution matrix.

func NewKernel

func NewKernel(width, height int) *Kernel

NewKernel returns a kernel of the provided length.

func (*Kernel) At

func (k *Kernel) At(x, y int) float64

At returns the matrix value at position x, y.

func (*Kernel) MaxX

func (k *Kernel) MaxX() int

MaxX returns the horizontal length.

func (*Kernel) MaxY

func (k *Kernel) MaxY() int

MaxY returns the vertical length.

func (*Kernel) Normalized

func (k *Kernel) Normalized() ConvolutionMatrix

Normalized returns a new Kernel with normalized values.

func (*Kernel) String

func (k *Kernel) String() string

String returns the string representation of the matrix.

type RGBAF64

type RGBAF64 struct {
	R, G, B, A float64
}

RGBAF64 represents an RGBA color using the range 0.0 to 1.0 with a float64 for each channel.

func NewRGBAF64

func NewRGBAF64(r, g, b, a uint8) RGBAF64

NewRGBAF64 returns a new RGBAF64 color based on the provided uint8 values.

func (*RGBAF64) Clamp

func (c *RGBAF64) Clamp()

Clamp limits the channel values of the RGBAF64 color to the range 0.0 to 1.0.

type RGBAHistogram

type RGBAHistogram struct {
	R Histogram
	G Histogram
	B Histogram
	A Histogram
}

RGBAHistogram holds a sub-histogram per RGBA channel. Each channel histogram contains 256 bins (8-bit color depth per channel).

func NewRGBAHistogram

func NewRGBAHistogram(img image.Image) *RGBAHistogram

NewRGBAHistogram constructs a RGBAHistogram out of the provided image. A sub-histogram is created per RGBA channel with 256 bins each.

func (*RGBAHistogram) Cumulative

func (h *RGBAHistogram) Cumulative() *RGBAHistogram

Cumulative returns a new RGBAHistogram in which each bin is the cumulative value of it's previous bins per channel.

func (*RGBAHistogram) Image

func (h *RGBAHistogram) Image() *image.RGBA

Image returns an RGBA image representation of the RGBAHistogram. An image width of 256 represents the 256 Bins per channel and the image height of 256 represents the max normalized histogram value per channel. Each RGB channel from the histogram is mapped to its corresponding channel in the image, so that for example if the red channel is extracted from the image, it corresponds to the red channel histogram.

type ResampleFilter

type ResampleFilter struct {
	Support float64
	Fn      func(x float64) float64
}

ResampleFilter is used to evaluate sample points and interpolate between them. Support is the number of points required by the filter per 'side'. For example, a support of 1.0 means that the filter will get pixels on positions -1 and +1 away from it. Fn is the resample filter function to evaluate the samples.

var Box ResampleFilter

Box resampling filter, only let pass values in the x < 0.5 range from sample. It produces similar results to the Nearest Neighbor method.

var CatmullRom ResampleFilter

CatmullRom resampling filter interpolates between the four nearest samples per dimension.

var Gaussian ResampleFilter

Gaussian resampling filter interpolates using a Gaussian function between the two nearest samples per dimension.

var Lanczos ResampleFilter

Lanczos resampling filter interpolates between the six nearest samples per dimension.

var Linear ResampleFilter

Linear resampling filter interpolates linearly between the two nearest samples per dimension.

var MitchellNetravali ResampleFilter

MitchellNetravali resampling filter interpolates between the four nearest samples per dimension.

var NearestNeighbor ResampleFilter

NearestNeighbor resampling filter assigns to each point the sample point nearest to it.

type RotationOptions

type RotationOptions struct {
	ResizeBounds bool
	Pivot        *image.Point
}

RotationOptions are the rotation parameters ResizeBounds set to false will keep the original image bounds, cutting any pixels that go past it when rotating. Pivot is the point of anchor for the rotation. Default of center is used if a nil is passed. If ResizeBounds is set to true, a center pivot will always be used.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL