Documentation
¶
Overview ¶
Package images is a pure-Go (cgo-free) image-processing library in the style of scikit-image, built on the Go standard library's image, image/color, image/png and image/jpeg packages.
Unlike the established options in the Ruby world — ruby-vips (libvips) and RMagick (ImageMagick) — and unlike cgo-backed Go wrappers, this library has no native C dependency: it is CGO=0 and builds and runs identically on every Go target. That makes it trivially cross-compilable and embeddable (for example inside go-embedded-ruby), at the cost — for now — of the hand-tuned SIMD that libvips/ImageMagick rely on. A later phase will close that gap with SIMD kernels generated by go-asmgen across the six supported 64-bit targets. See docs/plan-images.md for the roadmap and an honest comparison with the existing pure-Go libraries (bild, disintegration/imaging).
Operations are pure functions: each takes an image and returns a freshly allocated *image.RGBA, never mutating its input. Use ToRGBA to convert an arbitrary image.Image to the *image.RGBA the operations consume, and the I/O helpers (Load, Save, Decode, Encode). Decode auto-detects eight container formats — PNG and JPEG via the standard library, and GIF, WebP, TIFF, BMP, ICO and ICNS via the shared reference registry github.com/go-gfx/gfx/codec; Encode writes PNG and JPEG.
Phase 0 implements Grayscale, Invert, Resize (nearest-neighbour, bilinear, area/box, and the high-quality bicubic and Lanczos modes, all delegated to the shared go-gfx/gfx/resample foundation), Convolve (arbitrary odd-sized kernels with edge clamping), GaussianBlur (separable), AdjustBrightness and AdjustContrast.
Phase 1 adds edge detection — Sobel (gradient magnitude) with SobelX/SobelY directional responses, the normalised Prewitt, Scharr and SobelMag operators and the Laplacian (matching skimage.filters.{prewitt,scharr,sobel,laplace}), and the full Canny detector (Gaussian, Sobel, bilinear non-maximum suppression, hysteresis); the filters BoxBlur (separable running-sum mean, matching scipy.ndimage.uniform_filter), Median (square median, matching scipy.ndimage.median_filter), and UnsharpMask/Sharpen (matching skimage.filters.unsharp_mask); the geometric transforms FlipHorizontal, FlipVertical, Rotate90/Rotate180/Rotate270 (numpy.fliplr/flipud/rot90) and Crop; the colour conversions RGBToHSV/HSVToRGB; and thresholding via OtsuThreshold/Threshold/Otsu (matching skimage.filters.threshold_otsu).
Phase 2 adds grayscale morphology over a square structuring element: Erode and Dilate (per-channel local min/max, matching scipy.ndimage.grey_erosion and grey_dilation) and the derived Open and Close. On binary 0/255 images these reduce to ordinary binary morphology.
docs/perf.md reports honest go-images-vs-scikit-image/SciPy benchmarks and correctness checks for the hot operations.
Index ¶
- func AdjustBrightness(img image.Image, delta float64) *image.RGBA
- func AdjustBrightnessRaster(src *raster.Image, delta float64) *raster.Image
- func AdjustContrast(img image.Image, factor float64) *image.RGBA
- func AdjustContrastRaster(src *raster.Image, factor float64) *raster.Image
- func AsRGBA(r *raster.Image) *image.RGBA
- func AsRaster(img *image.RGBA) *raster.Image
- func BoxBlur(img image.Image, radius int) (*image.RGBA, error)
- func BoxBlurRaster(src *raster.Image, radius int) (*raster.Image, error)
- func Canny(img image.Image, sigma, low, high float64) (*image.RGBA, error)
- func CannyRaster(src *raster.Image, sigma, low, high float64) (*raster.Image, error)
- func Close(img image.Image, radius int) (*image.RGBA, error)
- func CloseRaster(src *raster.Image, radius int) (*raster.Image, error)
- func Convolve(img image.Image, k Kernel) (*image.RGBA, error)
- func ConvolveRaster(src *raster.Image, k Kernel) (*raster.Image, error)
- func Crop(img image.Image, r image.Rectangle) (*image.RGBA, error)
- func CropRaster(src *raster.Image, r image.Rectangle) (*raster.Image, error)
- func Decode(r io.Reader) (*image.RGBA, error)
- func DecodeBest(r io.Reader, targetSize int) (*image.RGBA, error)
- func DecodeExifTranspose(r io.Reader) (*image.RGBA, error)
- func DecodePartial(r io.Reader) (*image.RGBA, int, error)
- func DecodePartialFile(path string) (*image.RGBA, int, error)
- func Dilate(img image.Image, radius int) (*image.RGBA, error)
- func DilateRaster(src *raster.Image, radius int) (*raster.Image, error)
- func Encode(w io.Writer, img image.Image, format Format) error
- func Erode(img image.Image, radius int) (*image.RGBA, error)
- func ErodeRaster(src *raster.Image, radius int) (*raster.Image, error)
- func ExifTranspose(img image.Image, o Orientation) *image.RGBA
- func FlipHorizontal(img image.Image) *image.RGBA
- func FlipHorizontalRaster(src *raster.Image) *raster.Image
- func FlipVertical(img image.Image) *image.RGBA
- func FlipVerticalRaster(src *raster.Image) *raster.Image
- func GaussianBlur(img image.Image, sigma float64) (*image.RGBA, error)
- func GaussianBlurRaster(src *raster.Image, sigma float64) (*raster.Image, error)
- func Grayscale(img image.Image) *image.RGBA
- func GrayscaleRaster(src *raster.Image) *raster.Image
- func HSVToRGB(img image.Image) *image.RGBA
- func HSVToRGBRaster(src *raster.Image) *raster.Image
- func Invert(img image.Image) *image.RGBA
- func InvertRaster(src *raster.Image) *raster.Image
- func Laplacian(img image.Image) *image.RGBA
- func LaplacianRaster(src *raster.Image) *raster.Image
- func Load(path string) (*image.RGBA, error)
- func LoadExifTranspose(path string) (*image.RGBA, error)
- func Median(img image.Image, radius int) (*image.RGBA, error)
- func MedianRaster(src *raster.Image, radius int) (*raster.Image, error)
- func Open(img image.Image, radius int) (*image.RGBA, error)
- func OpenRaster(src *raster.Image, radius int) (*raster.Image, error)
- func Otsu(img image.Image) *image.RGBA
- func OtsuRaster(src *raster.Image) *raster.Image
- func OtsuThreshold(img image.Image) uint8
- func Prewitt(img image.Image) *image.RGBA
- func PrewittRaster(src *raster.Image) *raster.Image
- func RGBToHSV(img image.Image) *image.RGBA
- func RGBToHSVRaster(src *raster.Image) *raster.Image
- func Resize(img image.Image, w, h int, mode ResizeMode) (*image.RGBA, error)
- func ResizeRaster(src *raster.Image, w, h int, mode ResizeMode) (*raster.Image, error)
- func Rotate(img image.Image, angle float64, resize bool) *image.RGBA
- func Rotate90(img image.Image) *image.RGBA
- func Rotate90Raster(src *raster.Image) *raster.Image
- func Rotate180(img image.Image) *image.RGBA
- func Rotate180Raster(src *raster.Image) *raster.Image
- func Rotate270(img image.Image) *image.RGBA
- func Rotate270Raster(src *raster.Image) *raster.Image
- func Save(path string, img image.Image) error
- func Scharr(img image.Image) *image.RGBA
- func ScharrRaster(src *raster.Image) *raster.Image
- func Sharpen(img image.Image) *image.RGBA
- func SharpenRaster(src *raster.Image) *raster.Image
- func Sobel(img image.Image) *image.RGBA
- func SobelMag(img image.Image) *image.RGBA
- func SobelMagRaster(src *raster.Image) *raster.Image
- func SobelRaster(src *raster.Image) *raster.Image
- func SobelX(img image.Image) *image.RGBA
- func SobelXRaster(src *raster.Image) *raster.Image
- func SobelY(img image.Image) *image.RGBA
- func SobelYRaster(src *raster.Image) *raster.Image
- func Threshold(img image.Image, t uint8) *image.RGBA
- func ThresholdRaster(src *raster.Image, t uint8) *raster.Image
- func ToRGBA(img image.Image) *image.RGBA
- func Transpose(img image.Image) *image.RGBA
- func Transverse(img image.Image) *image.RGBA
- func UnsharpMask(img image.Image, radius, amount float64) (*image.RGBA, error)
- func UnsharpMaskRaster(src *raster.Image, radius, amount float64) (*raster.Image, error)
- func Warp(img image.Image, inv Affine, outW, outH int, interp Interp, mode BorderMode, ...) *image.RGBA
- type Affine
- type BorderMode
- type Format
- type Interp
- type Kernel
- type Orientation
- type ResizeMode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AdjustBrightness ¶
AdjustBrightness returns a copy of img with delta added to the R, G and B channels, clamped to [0, 255]. Alpha is preserved.
func AdjustBrightnessRaster ¶
AdjustBrightnessRaster is the raster-typed façade for AdjustBrightness.
func AdjustContrast ¶
AdjustContrast returns a copy of img with the R, G and B channels scaled about the mid-point (128) by factor, clamped to [0, 255]. A factor of 1 leaves the image unchanged; values above 1 increase contrast, values in [0, 1) reduce it. Alpha is preserved.
func AdjustContrastRaster ¶
AdjustContrastRaster is the raster-typed façade for AdjustContrast.
func AsRGBA ¶
AsRGBA returns an *image.RGBA that shares r's pixel buffer without copying.
go-gfx's raster.Image (the shared 2-D substrate this library sits on top of) and the standard library's image.RGBA use the identical physical layout: a densely packed, origin-anchored, row-major slice of four bytes (R, G, B, A) per pixel with stride 4*W and no row padding. The two therefore alias one backing array. The only nominal difference is the alpha model — raster.Image is straight (non-premultiplied) whereas image.RGBA documents its bytes as premultiplied — but every operation in this package treats the bytes as straight per-channel values and preserves the alpha channel unchanged, so the aliased view is processed exactly as raster's own straight bytes would be. Writing through either view is visible through the other.
func AsRaster ¶
AsRaster returns a raster.Image viewing img's pixels. When img is densely packed and origin-anchored (Rect.Min at the origin and Stride == 4*width) the returned raster shares img's backing array with no copy, the exact inverse of AsRGBA; otherwise — a sub-image, a padded stride, or a non-origin rectangle — the pixels are compacted into a freshly allocated, tightly packed buffer so the result always satisfies raster.Image's dense, origin-anchored invariant.
func BoxBlur ¶
BoxBlur returns img blurred by a square averaging filter of the given radius: every output pixel is the mean of the (2*radius+1) by (2*radius+1) source neighbourhood centred on it, computed independently per R, G and B channel (alpha preserved). Borders use clamp-to-edge addressing, matching scipy.ndimage.uniform_filter with mode="nearest" and size 2*radius+1. The filter is separable and evaluated with a running window sum, so its cost is independent of the radius. It returns an error if radius is not positive.
func BoxBlurRaster ¶
BoxBlurRaster is the raster-typed façade for BoxBlur; it propagates BoxBlur's error for a non-positive radius.
func Canny ¶
Canny returns the binary Canny edge map of img: white (255,255,255) edges on an opaque black background. It implements the classic Canny pipeline, matching the algorithm of skimage.feature.canny:
- smooth the Rec. 601 luminance with a Gaussian of standard deviation sigma (clamp-to-edge borders);
- estimate gradients with the Sobel operator; the edge strength is the gradient norm;
- thin to 1-pixel ridges by non-maximum suppression with bilinear interpolation along the gradient direction;
- link edges by hysteresis: keep every ridge pixel with magnitude >= high, plus every ridge pixel with magnitude >= low that is 8-connected to a kept one.
low and high are absolute thresholds on the Sobel gradient magnitude (the smoothed luminance is in [0,255], so the magnitudes are on that scale). It returns an error if sigma is not positive, if either threshold is negative, or if high < low.
func CannyRaster ¶
CannyRaster is the raster-typed façade for Canny; it propagates Canny's error for a non-positive sigma, a negative threshold, or high < low.
func Close ¶
Close returns the morphological closing of img (dilation followed by erosion). Closing fills small dark features smaller than the structuring element. It returns an error if radius is not positive.
func CloseRaster ¶
CloseRaster is the raster-typed façade for Close; it propagates Close's error for a non-positive radius.
func Convolve ¶
Convolve returns img convolved with k, using clamp-to-edge addressing at the borders. The R, G and B channels are convolved and clamped to [0, 255]; alpha is preserved. It returns an error if k has non-positive or even dimensions, or if the length of k.Weights does not match Width*Height.
func ConvolveRaster ¶
ConvolveRaster is the raster-typed façade for Convolve; it propagates Convolve's error for a kernel with non-positive, even, or mismatched dimensions.
func Crop ¶
Crop returns the rectangular region r of img as a new image anchored at the origin. r is interpreted in the coordinate system of img converted to RGBA (origin at the top-left). It returns an error if r is empty or extends outside the image bounds.
func CropRaster ¶
CropRaster is the raster-typed façade for Crop; it propagates Crop's error for an empty rectangle or one extending outside the image bounds.
func Decode ¶
Decode reads an image from r, auto-detecting the container format from its magic bytes, and returns it converted to *image.RGBA.
Eight formats are recognised. PNG and JPEG are decoded by the standard library exactly as before — this path is byte-for-byte unchanged. The six additional formats (GIF, WebP, TIFF, BMP, ICO, ICNS) are delegated to the shared reference registry github.com/go-gfx/gfx/codec, which reimplements no decoder and hands each container to a battle-tested pure-Go (CGO-free) library. For the multi-representation containers (ICO, ICNS) the largest representation is returned; use DecodeBest to target a pixel size.
Alpha convention: every source is brought into this package under the same premultiplied-at-input convention that ToRGBA already applies to any non-*image.RGBA source. codec returns straight (non-premultiplied) alpha, so its output is routed back through ToRGBA to premultiply, keeping the whole library's decoded bytes consistent regardless of container format.
func DecodeBest ¶
DecodeBest is Decode with a target pixel size for the multi-representation containers (ICO, ICNS): among their stored representations it selects the one whose longer side is the smallest that is still at least targetSize, falling back to the largest when none reaches it. A targetSize <= 0 selects the largest, making it identical to Decode. targetSize is ignored for single-image formats. The result is a premultiplied *image.RGBA, exactly as from Decode.
func DecodeExifTranspose ¶
DecodeExifTranspose decodes an image from r and returns it already re-oriented according to its embedded EXIF orientation tag, as a premultiplied *image.RGBA. It is the auto-orienting counterpart of Decode: an image with no EXIF orientation (or a non-JPEG/TIFF source) is returned unrotated. This is the equivalent of Pillow's ImageOps.exif_transpose applied at decode time.
func DecodePartial ¶
DecodePartial reads an image from r that may still be arriving, and returns what of it is real: the picture at its full declared size, and the number of pixel rows that are COMPLETE counting from the top. Rows past the count hold whatever the image was allocated with, so a caller draws the first rows and nothing else.
The error that stopped the decode comes back with them, and is nil only when the whole image arrived — in which case the count is every row.
⛔ Every standard decoder is all-or-nothing, which for a file still arriving is the same as having nothing. Measured on a real photograph truncated at three fractions, image/jpeg, image/png and image/gif each returned a nil image and an error EVERY time, with three quarters of the picture on disk. This is why the three forks behind this call exist.
Three of the eight formats Decode recognises can answer, and each refuses some shapes of its own — an interlaced PNG or GIF, a progressive or CMYK JPEG — for reasons written where the refusal is made:
- PNG, by github.com/go-images/png
- JPEG, by github.com/go-images/jpeg
- GIF, by github.com/go-images/gif (its first frame)
The other five (WebP, TIFF, BMP, ICO, ICNS) report that they cannot. WebP is the one that could not simply be forked the same way: its frame is a VP8 keyframe, decoded as a whole rather than row by row, so there is no partial state to hand back.
⛔ One divergence from Decode, stated rather than hidden. On a COMPLETE file this returns the FORK's decode, and for a four-component JPEG that differs: the fork upsamples chroma the way libjpeg does, where the standard library repeats each sample. See github.com/go-images/jpeg. Decode's standard-library path is untouched, so anything relying on its exact bytes still gets them.
func DecodePartialFile ¶
DecodePartialFile is DecodePartial on a path, for the common case of watching a file grow on disk.
func Dilate ¶
Dilate returns the grayscale morphological dilation of img: the per-channel local maximum over a square structuring element. See Erode for borders, alpha and the radius rule.
func DilateRaster ¶
DilateRaster is the raster-typed façade for Dilate; it propagates Dilate's error for a non-positive radius.
func Erode ¶
Erode returns the grayscale morphological erosion of img with a square structuring element of the given radius: every output channel is the minimum of the (2*radius+1) by (2*radius+1) source neighbourhood. Borders use clamp-to-edge addressing; alpha is preserved. The operation is separable (matching a square footprint in scipy.ndimage.grey_erosion). On a binary image (0/255) this is ordinary binary erosion. It returns an error if radius is not positive.
func ErodeRaster ¶
ErodeRaster is the raster-typed façade for Erode; it propagates Erode's error for a non-positive radius.
func ExifTranspose ¶
func ExifTranspose(img image.Image, o Orientation) *image.RGBA
ExifTranspose returns a copy of img re-oriented so that it displays the right way up, applying the geometric transform that the EXIF orientation o calls for. It mirrors Pillow's ImageOps.exif_transpose geometry:
1 identity 2 flip horizontal 3 rotate 180 4 flip vertical 5 transpose 6 rotate 90 CW 7 transverse 8 rotate 90 CCW
Orientations 5-8 swap the width and height. Any value outside 1-8 (including a zero Orientation) is treated as OrientationNormal and returns an unrotated RGBA copy, matching Pillow's lenient handling of an absent or invalid tag.
func FlipHorizontal ¶
FlipHorizontal returns a copy of img mirrored left-to-right (column x of the width-w source becomes column w-1-x). It matches numpy.fliplr.
func FlipHorizontalRaster ¶
FlipHorizontalRaster is the raster-typed façade for FlipHorizontal.
func FlipVertical ¶
FlipVertical returns a copy of img mirrored top-to-bottom (row y of the height-h source becomes row h-1-y). It matches numpy.flipud.
func FlipVerticalRaster ¶
FlipVerticalRaster is the raster-typed façade for FlipVertical.
func GaussianBlur ¶
GaussianBlur returns img blurred by a Gaussian of standard deviation sigma, implemented as a separable convolution with clamp-to-edge borders. It returns an error if sigma is not positive.
func GaussianBlurRaster ¶
GaussianBlurRaster is the raster-typed façade for GaussianBlur; it propagates GaussianBlur's error for a non-positive sigma.
func Grayscale ¶
Grayscale returns a copy of img with every pixel replaced by its luminance-weighted gray value (Rec. 601 coefficients). Alpha is preserved.
func GrayscaleRaster ¶
GrayscaleRaster is the raster-typed façade for Grayscale.
func HSVToRGB ¶
HSVToRGB inverts RGBToHSV: it interprets each pixel's first three channels as byte-encoded H, S, V and returns the corresponding R, G, B. Alpha is preserved.
func HSVToRGBRaster ¶
HSVToRGBRaster is the raster-typed façade for HSVToRGB.
func Invert ¶
Invert returns a copy of img with the R, G and B channels negated. Alpha is preserved.
func InvertRaster ¶
InvertRaster is the raster-typed façade for Invert.
func Laplacian ¶
Laplacian returns the discrete Laplacian edge map of img, matching skimage.filters.laplace with ksize=3 (the kernel [0,-1,0; -1,4,-1; 0,-1,0] applied to the luminance plane). The signed second-derivative response is offset by 128 so a flat region is mid-grey, written to R, G and B and clamped to [0,255]; alpha is preserved and borders use clamp-to-edge addressing. Being a second-derivative operator it highlights intensity curvature (lines, spots, zero-crossings) rather than step edges.
func LaplacianRaster ¶
LaplacianRaster is the raster-typed façade for Laplacian.
func Load ¶
Load reads and decodes the image at path, returning it as *image.RGBA. The format is auto-detected from the file contents.
func LoadExifTranspose ¶
LoadExifTranspose reads the image at path and returns it re-oriented per its EXIF orientation tag, as a premultiplied *image.RGBA. It is the auto-orienting counterpart of Load.
func Median ¶
Median returns img filtered by a square median of the given radius: every output channel is the median of the (2*radius+1) by (2*radius+1) source neighbourhood, computed independently per R, G and B (alpha preserved), with clamp-to-edge addressing. It matches scipy.ndimage.median_filter with a (2*radius+1)-square footprint and mode="nearest". The median is robust to outliers, so it removes salt-and-pepper noise while preserving edges far better than a linear blur. It returns an error if radius is not positive.
func MedianRaster ¶
MedianRaster is the raster-typed façade for Median; it propagates Median's error for a non-positive radius.
func Open ¶
Open returns the morphological opening of img (erosion followed by dilation with the same square structuring element). Opening removes small bright features smaller than the element while preserving overall shape. It returns an error if radius is not positive.
func OpenRaster ¶
OpenRaster is the raster-typed façade for Open; it propagates Open's error for a non-positive radius.
func Otsu ¶
Otsu is a convenience wrapper that thresholds img at the level chosen by Otsu's method (equivalent to Threshold(img, OtsuThreshold(img))).
func OtsuRaster ¶
OtsuRaster is the raster-typed façade for Otsu.
func OtsuThreshold ¶
OtsuThreshold returns the gray level in [0, 255] computed by Otsu's method on img's Rec. 601 luminance histogram: the level that maximises the between-class variance of the two pixel populations split at it. It matches the value returned by skimage.filters.threshold_otsu on a 256-bin histogram. Pass the result to Threshold (foreground = luminance strictly greater than it).
func Prewitt ¶
Prewitt returns the Prewitt gradient-magnitude edge map of img. The operator is the separable 3x3 Prewitt kernel applied to each pixel's Rec. 601 luminance; the magnitude sqrt((gx^2+gy^2)/2) is written to the R, G and B channels as a grayscale edge image (alpha preserved). It mirrors skimage.filters.prewitt: the directional kernels are normalised so each axis kernel's absolute weights sum to one, and clamp-to-edge addressing reproduces skimage's default reflect border for a 3-tap kernel.
func PrewittRaster ¶
PrewittRaster is the raster-typed façade for Prewitt.
func RGBToHSV ¶
RGBToHSV returns a copy of img with each pixel's R, G, B replaced by a byte-encoded H, S, V triple: H is the hue mapped from [0,360) to [0,255], S and V are mapped from [0,1] to [0,255]. Alpha is preserved. HSVToRGB inverts the mapping (within rounding). The encoding keeps the result inside the same RGBA-backed representation the rest of the pipeline uses.
func RGBToHSVRaster ¶
RGBToHSVRaster is the raster-typed façade for RGBToHSV.
func Resize ¶
Resize returns img scaled to w by h pixels using the given mode. It returns an error if w or h is not positive.
The resampling kernels live once, in go-gfx's resample package (the shared 2-D foundation this library sits on); Resize delegates to them over a zero-copy raster view of the source so no kernel is duplicated here. NearestNeighbor, Bilinear and Area map to resample's Nearest, Bilinear and Box filtered in straight-alpha space — proven byte-for-byte identical to the kernels this library used to run (see resize_dedup_control_test.go) — while Bicubic and Lanczos filter the colour channels in premultiplied-alpha space.
func ResizeRaster ¶
ResizeRaster scales src to w by h pixels using mode, accepting and returning go-gfx's raster.Image so callers already working in the shared substrate do not have to round-trip through image.RGBA. It is exactly Resize evaluated over an aliased view of src: Resize's freshly allocated destination is itself dense and origin-anchored, so both the input and the output are aliased with no extra copy, and the resulting pixel bytes are identical to those Resize produces for the same source. It returns an error if w or h is not positive, or for an unknown mode.
func Rotate ¶
Rotate returns img rotated by angle degrees counter-clockwise about its centre, reproducing skimage.transform.rotate with bilinear interpolation, the "constant" border and a fill value of zero. When resize is false the output keeps the input's dimensions (corners may be clipped); when true the output grows to contain the whole rotated image, exactly as scikit-image sizes it.
It matches scikit-image's rotate with clip=False: the interpolated result is not clamped to the input's value range, so a border pixel fades toward the fill value rather than being pinned to the input's global minimum. (Its clip=True default clamps every channel to the image-wide [min, max], which pins a fade-to-black border up to the minimum — the less useful behaviour.)
func Rotate90 ¶
Rotate90 returns img rotated 90 degrees counter-clockwise (matching numpy.rot90 with k=1). A w-by-h image becomes h-by-w.
func Rotate90Raster ¶
Rotate90Raster is the raster-typed façade for Rotate90.
func Rotate180 ¶
Rotate180 returns img rotated 180 degrees (numpy.rot90 with k=2). The dimensions are unchanged.
func Rotate180Raster ¶
Rotate180Raster is the raster-typed façade for Rotate180.
func Rotate270 ¶
Rotate270 returns img rotated 90 degrees clockwise, i.e. 270 degrees counter-clockwise (numpy.rot90 with k=3). A w-by-h image becomes h-by-w.
func Rotate270Raster ¶
Rotate270Raster is the raster-typed façade for Rotate270.
func Save ¶
Save encodes img and writes it to path, choosing the format from the file extension: ".png" for PNG and ".jpg" or ".jpeg" for JPEG (case-insensitive). It returns an error for any other extension.
func Scharr ¶
Scharr returns the Scharr gradient-magnitude edge map of img, matching skimage.filters.scharr. The Scharr smoothing triple (0.1875, 0.625, 0.1875) gives the best rotational symmetry of the Sobel/Prewitt/Scharr family. See Prewitt for the luminance, magnitude and border conventions.
func ScharrRaster ¶
ScharrRaster is the raster-typed façade for Scharr.
func Sharpen ¶
Sharpen returns a sharpened copy of img with sensible defaults: an unsharp mask with radius 1.0 and amount 1.0, i.e. it adds back the full single-pixel- scale detail layer. For finer control over the scale or strength use UnsharpMask directly.
func SharpenRaster ¶
SharpenRaster is the raster-typed façade for Sharpen.
func Sobel ¶
Sobel returns the Sobel gradient-magnitude edge map of img. The operator is applied to each pixel's Rec. 601 luminance with clamp-to-edge borders; the magnitude is clamped to [0, 255] and written to the R, G and B channels, producing a grayscale edge image. Alpha is preserved. Strong intensity transitions appear bright, flat regions dark.
func SobelMag ¶
SobelMag returns the normalised Sobel gradient-magnitude edge map of img using the scikit-image convention (sqrt((gx^2+gy^2)/2) on luminance scaled to [0,1]), matching skimage.filters.sobel. It differs from Sobel, which uses the classic integer kernels and the unnormalised magnitude sqrt(gx^2+gy^2); SobelMag shares the one definition used by Prewitt and Scharr so the edge family is directly comparable to scikit-image.
func SobelMagRaster ¶
SobelMagRaster is the raster-typed façade for SobelMag.
func SobelRaster ¶
SobelRaster is the raster-typed façade for Sobel.
func SobelX ¶
SobelX returns the horizontal Sobel response of img: an estimate of the left-to-right intensity derivative of each pixel's luminance. The signed response is scaled and offset so a zero gradient is mid-grey (128), a rising edge brighter and a falling edge darker, clamped to [0, 255] and written to R, G and B. Alpha is preserved; borders use clamp-to-edge addressing.
func SobelXRaster ¶
SobelXRaster is the raster-typed façade for SobelX.
func SobelY ¶
SobelY returns the vertical Sobel response of img: an estimate of the top-to-bottom intensity derivative of each pixel's luminance, with the same scaling, offset and addressing conventions as SobelX. Alpha is preserved.
func SobelYRaster ¶
SobelYRaster is the raster-typed façade for SobelY.
func Threshold ¶
Threshold returns a binary image: every pixel of img whose Rec. 601 luminance is strictly greater than t becomes white, every other pixel black. Alpha is preserved. Combine with OtsuThreshold for an automatically chosen level.
func ThresholdRaster ¶
ThresholdRaster is the raster-typed façade for Threshold.
func ToRGBA ¶
ToRGBA returns img as an *image.RGBA. If img is already an *image.RGBA whose bounds start at the origin, it is returned unchanged; otherwise the pixels are copied (and, when necessary, colour-converted) into a freshly allocated origin-anchored *image.RGBA of the same dimensions.
func Transpose ¶
Transpose returns img reflected across its main diagonal (top-left to bottom-right): pixel (x, y) becomes (y, x). A w-by-h image becomes h-by-w. It matches PIL Image.TRANSPOSE and is the geometry of EXIF orientation 5.
func Transverse ¶
Transverse returns img reflected across its anti-diagonal (top-right to bottom-left): pixel (x, y) becomes (h-1-y, w-1-x). A w-by-h image becomes h-by-w. It matches PIL Image.TRANSVERSE and is the geometry of EXIF orientation 7.
func UnsharpMask ¶
UnsharpMask returns a sharpened copy of img using the unsharp-masking technique: dst = clamp(src + amount*(src - blurred)), where blurred is the Gaussian blur of img with standard deviation radius. The R, G and B channels are processed independently and alpha is preserved. It matches skimage.filters.unsharp_mask applied per channel.
radius controls the scale of the detail recovered (the Gaussian sigma) and must be positive; amount scales how strongly that detail is added back: 0 leaves the image unchanged, typical sharpening uses values around 0.5–2, and negative values soften. It returns an error if radius is not positive.
func UnsharpMaskRaster ¶
UnsharpMaskRaster is the raster-typed façade for UnsharpMask; it propagates UnsharpMask's error for a non-positive radius.
func Warp ¶
func Warp(img image.Image, inv Affine, outW, outH int, interp Interp, mode BorderMode, cval float64) *image.RGBA
Warp resamples img through the inverse coordinate map inv onto an outW-by-outH output, matching skimage.transform.warp. inv maps an output pixel's (col, row) to the input (col, row) to sample, so it is the inverse of the geometric transform being applied to the image (exactly as scikit-image's warp takes an inverse map). interp selects nearest or bilinear sampling; mode and cval select the out-of-bounds behaviour. cval fills the R, G and B channels outside the image; the alpha channel is filled with 0 there, so out-of-bounds regions are transparent regardless of cval.
Types ¶
type Affine ¶
type Affine struct {
A, B, C, D, E, F float64
}
Affine is a 2-D affine transform. It maps a point (x, y) — with x the column and y the row, matching scikit-image's coordinate order — to
x' = A*x + B*y + C y' = D*x + E*y + F
which is the top two rows of the homogeneous 3x3 matrix whose bottom row is (0, 0, 1).
func Identity ¶
func Identity() Affine
Identity is the affine transform that leaves every point unchanged.
func Rotation ¶
Rotation returns a transform that rotates a point about the origin by theta radians, using scikit-image's convention ([[cos, -sin], [sin, cos]] acting on (x, y)).
func Translation ¶
Translation returns a transform that shifts a point by (tx, ty).
type BorderMode ¶
type BorderMode int
BorderMode selects how samples that fall outside the input image are filled.
const ( // BorderConstant fills out-of-bounds samples with a constant value // (scikit-image mode "constant" with cval). BorderConstant BorderMode = iota // BorderEdge clamps out-of-bounds coordinates to the nearest edge pixel // (scikit-image mode "edge"). BorderEdge )
type Interp ¶
type Interp int
Interp selects the interpolation order used when sampling between pixels.
type Kernel ¶
Kernel is a 2-D convolution kernel: Weights is a row-major slice of length Width*Height, and both Width and Height must be odd.
type Orientation ¶
type Orientation uint8
Orientation is the EXIF image-orientation tag (TIFF tag 0x0112). Its eight legal values, 1 through 8, each describe how the stored pixels must be transformed to be displayed the right way up. ExifTranspose applies the matching transform. OrientationNormal (1) is the identity.
const ( // OrientationNormal (1) needs no transform. OrientationNormal Orientation = 1 // OrientationFlipHorizontal (2) is mirrored left-to-right. OrientationFlipHorizontal Orientation = 2 // OrientationRotate180 (3) is rotated 180 degrees. OrientationRotate180 Orientation = 3 // OrientationFlipVertical (4) is mirrored top-to-bottom. OrientationFlipVertical Orientation = 4 // OrientationTranspose (5) is reflected across the main diagonal. OrientationTranspose Orientation = 5 // OrientationRotate90CW (6) needs a 90-degree clockwise rotation. OrientationRotate90CW Orientation = 6 // OrientationTransverse (7) is reflected across the anti-diagonal. OrientationTransverse Orientation = 7 // OrientationRotate90CCW (8) needs a 90-degree counter-clockwise rotation. OrientationRotate90CCW Orientation = 8 )
The eight EXIF orientation values, named for the transform each one calls for. The comment on each is the geometric operation ExifTranspose applies.
func OrientationFromExif ¶
func OrientationFromExif(data []byte) Orientation
OrientationFromExif extracts the EXIF orientation tag from an encoded image's bytes (a JPEG APP1/Exif segment, or a bare TIFF stream). It is deliberately lenient: when no EXIF block, no orientation tag, or a malformed structure is found, it returns OrientationNormal so callers can auto-orient unconditionally. A value stored outside the legal 1-8 range is also normalised to OrientationNormal.
type ResizeMode ¶
type ResizeMode int
ResizeMode selects the interpolation used by Resize.
const ( // NearestNeighbor selects the source pixel nearest to each destination // pixel. It is fast and exact for integer scale factors but blocky. NearestNeighbor ResizeMode = iota // Bilinear linearly interpolates the four nearest source pixels. It is // smoother than nearest-neighbour at the cost of more arithmetic. Bilinear // Area averages the source region each destination pixel covers, weighting // every source pixel by how much of it falls inside. It is the mode to // REDUCE with: nearest-neighbour discards all but one source pixel per // destination pixel, so shrinking by four throws away fifteen sixteenths of // the image and aliases what is left, while bilinear only ever looks at // four neighbours however far the image is shrunk. Enlarging, where each // destination pixel falls inside one source pixel, it reduces to // nearest-neighbour. // // This is PIL's Image.BOX and OpenCV's INTER_AREA; at integer ratios it // reduces to scikit-image's downscale_local_mean. Area // Bicubic resamples with the Keys cubic (a = -1/2, the Catmull-Rom spline), // a smooth four-tap kernel whose footprint widens with the reduction factor: // sharper than Bilinear when enlarging and a proper antialiasing low-pass // when reducing. It is Pillow's BICUBIC / x/image/draw's CatmullRom. The // colour channels are filtered in premultiplied-alpha space so a transparent // pixel's colour does not bleed into the visible edge of a cut-out; on a // fully opaque image that is exactly the plain cubic. Higher quality than the // three modes above, at more arithmetic. Bicubic // Lanczos resamples with the a = 3 windowed-sinc kernel: wider and sharper // than Bicubic at more cost, the highest-fidelity mode in either direction. // It is Pillow's LANCZOS / x/image/draw's Lanczos, and like Bicubic filters // the colour channels in premultiplied-alpha space. Lanczos )
