imaging

package
v0.0.0-...-5d2067b Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2021 License: MIT, MIT Imports: 20 Imported by: 0

Documentation

Overview

Package imaging provides basic image processing functions (resize, rotate, crop, brightness/contrast adjustments, etc.).

All the image processing functions provided by the package accept any image type that implements image.Image interface as an input, and return a new image of *image.NRGBA type (32bit RGBA colors, not premultiplied by alpha).

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnsupportedFormat means the given image format (or file extension) is unsupported.
	ErrUnsupportedFormat = errors.New("imaging: unsupported image format")
)

Functions

func AdjustBrightness

func AdjustBrightness(img image.Image, percentage float64) *image.NRGBA

AdjustBrightness changes the brightness of the image using the percentage parameter and returns the adjusted image. The percentage must be in range (-100, 100). The percentage = 0 gives the original image. The percentage = -100 gives solid black image. The percentage = 100 gives solid white image.

Examples:

dstImage = imaging.AdjustBrightness(srcImage, -15) // decrease image brightness by 15%
dstImage = imaging.AdjustBrightness(srcImage, 10) // increase image brightness by 10%

func AdjustContrast

func AdjustContrast(img image.Image, percentage float64) *image.NRGBA

AdjustContrast changes the contrast of the image using the percentage parameter and returns the adjusted image. The percentage must be in range (-100, 100). The percentage = 0 gives the original image. The percentage = -100 gives solid gray image.

Examples:

dstImage = imaging.AdjustContrast(srcImage, -10) // decrease image contrast by 10%
dstImage = imaging.AdjustContrast(srcImage, 20) // increase image contrast by 20%

func AdjustFunc

func AdjustFunc(img image.Image, fn func(c color.NRGBA) color.NRGBA) *image.NRGBA

AdjustFunc applies the fn function to each pixel of the img image and returns the adjusted image.

Example:

dstImage = imaging.AdjustFunc(
	srcImage,
	func(c color.NRGBA) color.NRGBA {
		// shift the red channel by 16
		r := int(c.R) + 16
		if r > 255 {
			r = 255
		}
		return color.NRGBA{uint8(r), c.G, c.B, c.A}
	}
)

func AdjustGamma

func AdjustGamma(img image.Image, gamma float64) *image.NRGBA

AdjustGamma performs a gamma correction on the image and returns the adjusted image. Gamma parameter must be positive. Gamma = 1.0 gives the original image. Gamma less than 1.0 darkens the image and gamma greater than 1.0 lightens it.

Example:

dstImage = imaging.AdjustGamma(srcImage, 0.7)

func AdjustHSL

func AdjustHSL(img image.Image, weight float64, h, s, l float64) *image.NRGBA

func AdjustHSLAFunc

func AdjustHSLAFunc(img image.Image, f func(x, y int, h, s, l, a *float64, seed *int)) *image.NRGBA

func AdjustHue

func AdjustHue(img image.Image, hue func(x, y int) float64) *image.NRGBA

func AdjustHueRotate

func AdjustHueRotate(img image.Image, delta func(int, int) float64) *image.NRGBA

func AdjustNoiseHSL

func AdjustNoiseHSL(img image.Image, hn, sn, ln float64) *image.NRGBA

func AdjustSaturation

func AdjustSaturation(img image.Image, factor float64) *image.NRGBA

func AdjustSigmoid

func AdjustSigmoid(img image.Image, midpoint, factor float64) *image.NRGBA

AdjustSigmoid changes the contrast of the image using a sigmoidal function and returns the adjusted image. It's a non-linear contrast change useful for photo adjustments as it preserves highlight and shadow detail. The midpoint parameter is the midpoint of contrast that must be between 0 and 1, typically 0.5. The factor parameter indicates how much to increase or decrease the contrast, typically in range (-10, 10). If the factor parameter is positive the image contrast is increased otherwise the contrast is decreased.

Examples:

dstImage = imaging.AdjustSigmoid(srcImage, 0.5, 3.0) // increase the contrast
dstImage = imaging.AdjustSigmoid(srcImage, 0.5, -3.0) // decrease the contrast

func AdjustTint

func AdjustTint(img image.Image, weight float64, tint color.RGBA) *image.NRGBA

func AnchorPoint

func AnchorPoint(i image.Image, anchor Anchor) image.Point

func Blur

func Blur(img image.Image, sigma float64) *image.NRGBA

Blur produces a blurred version of the image using a Gaussian function. Sigma parameter must be positive and indicates how much the image will be blurred.

Usage example:

dstImage := imaging.Blur(srcImage, 3.5)

func Clone

func Clone(img image.Image) *image.NRGBA

Clone returns a copy of the given image.

func Convolve3x3

func Convolve3x3(img image.Image, kernel [9]float64, options *ConvolveOptions) *image.NRGBA

Convolve3x3 convolves the image with the specified 3x3 convolution kernel. Default parameters are used if a nil *ConvolveOptions is passed.

func Convolve5x5

func Convolve5x5(img image.Image, kernel [25]float64, options *ConvolveOptions) *image.NRGBA

Convolve5x5 convolves the image with the specified 5x5 convolution kernel. Default parameters are used if a nil *ConvolveOptions is passed.

func Crop

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

Crop cuts out a rectangular region with the specified bounds from the image and returns the cropped image.

func CropAnchor

func CropAnchor(img image.Image, width, height int, anchor Anchor) *image.NRGBA

CropAnchor cuts out a rectangular region with the specified size from the image using the specified anchor point and returns the cropped image.

func CropCenter

func CropCenter(img image.Image, width, height int) *image.NRGBA

CropCenter cuts out a rectangular region with the specified size from the center of the image and returns the cropped image.

func Decode

func Decode(r io.Reader, opts ...DecodeOption) (image.Image, error)

Decode reads an image from r.

func Encode

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

Encode writes the image img to w in the specified format (JPEG, PNG, GIF, TIFF or BMP).

func Fill

func Fill(img image.Image, width, height int, anchor Anchor, filter ResampleFilter) *image.NRGBA

Fill scales the image to the smallest possible size that will cover the specified dimensions, crops the resized image to the specified dimensions using the given anchor point and returns the transformed image.

Supported resample filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali, CatmullRom, BSpline, Gaussian, Lanczos, Hann, Hamming, Blackman, Bartlett, Welch, Cosine.

Usage example:

dstImage := imaging.Fill(srcImage, 800, 600, imaging.Center, imaging.Lanczos)

func Fit

func Fit(img image.Image, width, height int, filter ResampleFilter) *image.NRGBA

Fit scales down the image using the specified resample filter to fit the specified maximum width and height and returns the transformed image.

Supported resample filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali, CatmullRom, BSpline, Gaussian, Lanczos, Hann, Hamming, Blackman, Bartlett, Welch, Cosine.

Usage example:

dstImage := imaging.Fit(srcImage, 800, 600, imaging.Lanczos)

func FlipH

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

FlipH flips the image horizontally (from left to right) and returns the transformed image.

func FlipV

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

FlipV flips the image vertically (from top to bottom) and returns the transformed image.

func FriedDistortion1

func FriedDistortion1(img image.Image, position image.Point, a, b, c float64) *image.NRGBA

func Grayscale

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

Grayscale produces a grayscale version of the image.

func HSLA

func HSLA(c color.RGBA) (h, s, l, a float64)

func Histogram

func Histogram(img image.Image) [256]float64

Histogram returns a normalized histogram of an image.

Resulting histogram is represented as an array of 256 floats, where histogram[i] is a probability of a pixel being of a particular luminance i.

func Invert

func Invert(img image.Image) *image.NRGBA

Invert produces an inverted (negated) version of the image.

func New

func New(width, height int, fillColor color.Color) *image.NRGBA

New creates a new image with the specified width and height, and fills it with the specified color.

func OpIgnore

func OpIgnore(r1, g1, b1, a1, r2, g2, b2, a2 float64) (r, g, b, a float64)

func OpLighten

func OpLighten(r1, g1, b1, a1, r2, g2, b2, a2 float64) (r, g, b, a float64)

func OpMax

func OpMax(r1, g1, b1, a1, r2, g2, b2, a2 float64) (r, g, b, a float64)

func OpMaxAlpha

func OpMaxAlpha(r1, g1, b1, a1, r2, g2, b2, a2 float64) (r, g, b, a float64)

func OpMinAlpha

func OpMinAlpha(r1, g1, b1, a1, r2, g2, b2, a2 float64) (r, g, b, a float64)

func OpPlus

func OpPlus(r1, g1, b1, a1, r2, g2, b2, a2 float64) (r, g, b, a float64)

func OpReplace

func OpReplace(r1, g1, b1, a1, r2, g2, b2, a2 float64) (r, g, b, a float64)

func OpReplaceAlpha

func OpReplaceAlpha(r1, g1, b1, a1, r2, g2, b2, a2 float64) (r, g, b, a float64)

func OpaqueArea

func OpaqueArea(img image.Image, threshold uint8) int

OpaqueArea returns the opaque area (in pixels) of the image

func OpaqueBounds

func OpaqueBounds(img image.Image, threshold uint8) image.Rectangle

OpaqueBounds returns a bounding box for the given image

func OpaquePolygon

func OpaquePolygon(img image.Image, n int, threshold uint8) (out []image.Point)

OpaquePolygon returns a bounding polygon for the given image

func Open

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

Open loads an image from file.

Examples:

// Load an image from file.
img, err := imaging.Open("test.jpg")

// Load an image and transform it depending on the EXIF orientation tag (if present).
img, err := imaging.Open("test.jpg", imaging.AutoOrientation(true))

func Overlay

func Overlay(background, img image.Image, pos image.Point, opacity float64) *image.NRGBA

Overlay draws the img image over the background image at given position and returns the combined image. Opacity parameter is the opacity of the img image layer, used to compose the images, it must be from 0.0 to 1.0.

Usage examples:

// Draw spriteImage over backgroundImage at the given position (x=50, y=50).
dstImage := imaging.Overlay(backgroundImage, spriteImage, image.Pt(50, 50), 1.0)

// Blend two opaque images of the same size.
dstImage := imaging.Overlay(imageOne, imageTwo, image.Pt(0, 0), 0.5)

func OverlayCenter

func OverlayCenter(background, img image.Image, opacity float64) *image.NRGBA

OverlayCenter overlays the img image to the center of the background image and returns the combined image. Opacity parameter is the opacity of the img image layer, used to compose the images, it must be from 0.0 to 1.0.

func OverlayOnCanvas

func OverlayOnCanvas(w, h int, bgColor color.Color, images []struct {
	Image image.Image
	Point image.Point
}) *image.NRGBA

func OverlayWithOp

func OverlayWithOp(background, img image.Image, pos image.Point, op OverlayOp) *image.NRGBA

func Paste

func Paste(background, img image.Image, pos image.Point) *image.NRGBA

Paste pastes the img image to the background image at the specified position and returns the combined image.

func PasteCenter

func PasteCenter(background, img image.Image) *image.NRGBA

PasteCenter pastes the img image to the center of the background image and returns the combined image.

func RGBA

func RGBA(h, s, l, a float64) (c color.RGBA)

func Resize

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

Resize resizes the image to the specified width and height using the specified resampling filter and returns the transformed image. If one of width or height is 0, the image aspect ratio is preserved.

Supported resample filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali, CatmullRom, BSpline, Gaussian, Lanczos, Hann, Hamming, Blackman, Bartlett, Welch, Cosine.

Usage example:

dstImage := imaging.Resize(srcImage, 800, 600, imaging.Lanczos)

func Rotate

func Rotate(img image.Image, angle float64, bgColor color.Color) *image.NRGBA

Rotate rotates an image by the given angle counter-clockwise . The angle parameter is the rotation angle in degrees. The bgColor parameter specifies the color of the uncovered zone after the rotation.

func Rotate180

func Rotate180(img image.Image) *image.NRGBA

Rotate180 rotates the image 180 degrees counter-clockwise and returns the transformed image.

func Rotate270

func Rotate270(img image.Image) *image.NRGBA

Rotate270 rotates the image 270 degrees counter-clockwise and returns the transformed image.

func Rotate90

func Rotate90(img image.Image) *image.NRGBA

Rotate90 rotates the image 90 degrees counter-clockwise and returns the transformed image.

func RotateAbout

func RotateAbout(img image.Image, p image.Point, angle float64, bgColor color.Color) *image.NRGBA

func Save

func Save(img image.Image, filename string, opts ...EncodeOption) (err error)

Save saves the image to file with the specified filename. The format is determined from the filename extension: "jpg" (or "jpeg"), "png", "gif", "tif" (or "tiff") and "bmp" are supported.

Examples:

// Save the image as PNG.
err := imaging.Save(img, "out.png")

// Save the image as JPEG with optional quality parameter set to 80.
err := imaging.Save(img, "out.jpg", imaging.JPEGQuality(80))

func Sharpen

func Sharpen(img image.Image, sigma float64) *image.NRGBA

Sharpen produces a sharpened version of the image. Sigma parameter must be positive and indicates how much the image will be sharpened.

Usage example:

dstImage := imaging.Sharpen(srcImage, 3.5)

func Thumbnail

func Thumbnail(img image.Image, width, height int, filter ResampleFilter) *image.NRGBA

Thumbnail scales the image up or down using the specified resample filter, crops it to the specified width and hight and returns the transformed image.

Supported resample filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali, CatmullRom, BSpline, Gaussian, Lanczos, Hann, Hamming, Blackman, Bartlett, Welch, Cosine.

Usage example:

dstImage := imaging.Thumbnail(srcImage, 100, 100, imaging.Lanczos)

func Transpose

func Transpose(img image.Image) *image.NRGBA

Transpose flips the image horizontally and rotates 90 degrees counter-clockwise.

func Transverse

func Transverse(img image.Image) *image.NRGBA

Transverse flips the image vertically and rotates 90 degrees counter-clockwise.

Types

type Anchor

type Anchor int

Anchor is the anchor point for image alignment.

const (
	Center Anchor = iota
	TopLeft
	Top
	TopRight
	Left
	Right
	BottomLeft
	Bottom
	BottomRight
)

Anchor point positions.

type ConvolveOptions

type ConvolveOptions struct {
	// If Normalize is true the kernel is normalized before convolution.
	Normalize bool

	// If Abs is true the absolute value of each color channel is taken after convolution.
	Abs bool

	// Bias is added to each color channel value after convolution.
	Bias int
}

ConvolveOptions are convolution parameters.

type DecodeOption

type DecodeOption func(*decodeConfig)

DecodeOption sets an optional parameter for the Decode and Open functions.

func AutoOrientation

func AutoOrientation(enabled bool) DecodeOption

AutoOrientation returns a DecodeOption that sets the auto-orientation mode. If auto-orientation is enabled, the image will be transformed after decoding according to the EXIF orientation tag (if present). By default it's disabled.

type EncodeOption

type EncodeOption func(*encodeConfig)

EncodeOption sets an optional parameter for the Encode and Save functions.

func GIFDrawer

func GIFDrawer(drawer draw.Drawer) EncodeOption

GIFDrawer returns an EncodeOption that sets the drawer that is used to convert the source image to the desired palette of the GIF-encoded image.

func GIFNumColors

func GIFNumColors(numColors int) EncodeOption

GIFNumColors returns an EncodeOption that sets the maximum number of colors used in the GIF-encoded image. It ranges from 1 to 256. Default is 256.

func GIFQuantizer

func GIFQuantizer(quantizer draw.Quantizer) EncodeOption

GIFQuantizer returns an EncodeOption that sets the quantizer that is used to produce a palette of the GIF-encoded image.

func JPEGQuality

func JPEGQuality(quality int) EncodeOption

JPEGQuality returns an EncodeOption that sets the output JPEG quality. Quality ranges from 1 to 100 inclusive, higher is better. Default is 95.

func PNGCompressionLevel

func PNGCompressionLevel(level png.CompressionLevel) EncodeOption

PNGCompressionLevel returns an EncodeOption that sets the compression level of the PNG-encoded image. Default is png.DefaultCompression.

type Format

type Format int

Format is an image file format.

const (
	JPEG Format = iota
	PNG
	GIF
	TIFF
	BMP
)

Image file formats.

func FormatFromExtension

func FormatFromExtension(ext string) (Format, error)

FormatFromExtension parses image format from extension: "jpg" (or "jpeg"), "png", "gif", "tif" (or "tiff") and "bmp" are supported.

func FormatFromFilename

func FormatFromFilename(filename string) (Format, error)

FormatFromFilename parses image format from filename extension: "jpg" (or "jpeg"), "png", "gif", "tif" (or "tiff") and "bmp" are supported.

func (Format) String

func (f Format) String() string

type OverlayOp

type OverlayOp func(r1, g1, b1, a1, r2, g2, b2, a2 float64) (r, g, b, a float64)

func OpBlend

func OpBlend(opacity float64) OverlayOp

type ResampleFilter

type ResampleFilter struct {
	Support float64
	Kernel  func(float64) float64
}

ResampleFilter is a resampling filter struct. It can be used to define custom filters.

Supported resample filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali, CatmullRom, BSpline, Gaussian, Lanczos, Hann, Hamming, Blackman, Bartlett, Welch, Cosine.

General filter recommendations:

- Lanczos
	High-quality resampling filter for photographic images yielding sharp results.
	It's slower than cubic filters (see below).

- CatmullRom
	A sharp cubic filter. It's a good filter for both upscaling and downscaling if sharp results are needed.

- MitchellNetravali
	A high quality cubic filter that produces smoother results with less ringing artifacts than CatmullRom.

- BSpline
	A good filter if a very smooth output is needed.

- Linear
	Bilinear interpolation filter, produces reasonably good, smooth output.
	It's faster than cubic filters.

- Box
	Simple and fast averaging filter appropriate for downscaling.
	When upscaling it's similar to NearestNeighbor.

- NearestNeighbor
	Fastest resampling filter, no antialiasing.
var BSpline ResampleFilter

BSpline is a smooth cubic filter (BC-spline; B=1; C=0).

var Bartlett ResampleFilter

Bartlett is a Bartlett-windowed sinc filter (3 lobes).

var Blackman ResampleFilter

Blackman is a Blackman-windowed sinc filter (3 lobes).

var Box ResampleFilter

Box filter (averaging pixels).

var CatmullRom ResampleFilter

CatmullRom is a Catmull-Rom - sharp cubic filter (BC-spline; B=0; C=0.5).

var Cosine ResampleFilter

Cosine is a Cosine-windowed sinc filter (3 lobes).

var Gaussian ResampleFilter

Gaussian is a Gaussian blurring Filter.

var Hamming ResampleFilter

Hamming is a Hamming-windowed sinc filter (3 lobes).

var Hann ResampleFilter

Hann is a Hann-windowed sinc filter (3 lobes).

var Hermite ResampleFilter

Hermite cubic spline filter (BC-spline; B=0; C=0).

var Lanczos ResampleFilter

Lanczos filter (3 lobes).

var Linear ResampleFilter

Linear filter.

var MitchellNetravali ResampleFilter

MitchellNetravali is Mitchell-Netravali cubic filter (BC-spline; B=1/3; C=1/3).

var NearestNeighbor ResampleFilter

NearestNeighbor is a nearest-neighbor filter (no anti-aliasing).

var Welch ResampleFilter

Welch is a Welch-windowed sinc filter (parabolic window, 3 lobes).

Jump to

Keyboard shortcuts

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