imaging

package module
v1.8.19 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2026 License: MIT Imports: 40 Imported by: 28

README

Imaging

This is pure Go code that makes working with images actually useable on top of the Go stdlib. In addition to the usual PNG/JPEG/WebP/TIFF/BMP/GIF formats that have been supported forever, this package adds support for animated PNG, animated WebP, Google's new "jpegli" JPEG variant and all the netPBM image formats.

Additionally, this package support color management via ICC profiles and CICP metadata. Opening non-sRGB images automatically converts them to sRGB, so you don't have to think about it. It has full support for ICC v2 and v4 profiles embedded in all the image formats and is extensively tested against the little-cms library.

It also supports loading image metadata in EXIF format and automatically supports the EXIF orientation flag -- on image load the image is transformed based on that tag automatically.

It automatically falls back to ImageMagick when available, for image formats it does not support.

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

Installation

go get -u github.com/kovidgoyal/imaging

Documentation

https://pkg.go.dev/github.com/kovidgoyal/imaging

Quickstart

img, metadata, err := imaging.OpenAll(path, options...)
img.Resize(128, 128, imaging.Lanczos)
img.SaveAsPNG(path, mode)

There are also convenience scripts that demonstrate this library in action, note that these are mainly for development and as such they only use the pure Go code and do not fallback to ImageMagick:

./to-png some-image.whatever some-image.png
./to-frames some-animated-image.whatever some-animated-image.apng

Imaging supports image resizing using various resampling filters. The most notable ones:

  • Lanczos - A high-quality resampling filter for photographic images yielding sharp results.
  • CatmullRom - A sharp cubic filter that is faster than Lanczos filter while providing similar results.
  • MitchellNetravali - A cubic filter that produces smoother results with less ringing artifacts than CatmullRom.
  • Linear - Bilinear resampling filter, produces smooth output. 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.

The full list of supported filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali, CatmullRom, BSpline, Gaussian, Lanczos, Hann, Hamming, Blackman, Bartlett, Welch, Cosine. Custom filters can be created using ResampleFilter struct.

Resampling filters comparison

Original image:

srcImage

The same image resized from 600x400px to 150x100px using different resampling filters. From faster (lower quality) to slower (higher quality):

Filter Resize result
imaging.NearestNeighbor dstImage
imaging.Linear dstImage
imaging.CatmullRom dstImage
imaging.Lanczos dstImage
Gaussian Blur
dstImage := imaging.Blur(srcImage, 0.5)

Sigma parameter allows to control the strength of the blurring effect.

Original image Sigma = 0.5 Sigma = 1.5
srcImage dstImage dstImage
Sharpening
dstImage := imaging.Sharpen(srcImage, 0.5)

Sharpen uses gaussian function internally. Sigma parameter allows to control the strength of the sharpening effect.

Original image Sigma = 0.5 Sigma = 1.5
srcImage dstImage dstImage
Gamma correction
dstImage := imaging.AdjustGamma(srcImage, 0.75)
Original image Gamma = 0.75 Gamma = 1.25
srcImage dstImage dstImage
Contrast adjustment
dstImage := imaging.AdjustContrast(srcImage, 20)
Original image Contrast = 15 Contrast = -15
srcImage dstImage dstImage
Brightness adjustment
dstImage := imaging.AdjustBrightness(srcImage, 20)
Original image Brightness = 10 Brightness = -10
srcImage dstImage dstImage
Saturation adjustment
dstImage := imaging.AdjustSaturation(srcImage, 20)
Original image Saturation = 30 Saturation = -30
srcImage dstImage dstImage
Hue adjustment
dstImage := imaging.AdjustHue(srcImage, 20)
Original image Hue = 60 Hue = -60
srcImage dstImage dstImage

Acknowledgements

This is a fork of the un-maintained distraction/imaging project. The color management code was started out from mandykoh/prism and used some code from go-andiamo/iccarus but it was almost completely re-written from scratch.

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, non-premultiplied alpha).

Example
package main

import (
	"image"
	"image/color"
	"log"

	"github.com/kovidgoyal/imaging"
)

func main() {
	// Open a test image.
	src, err := imaging.Open("testdata/flowers.png")
	if err != nil {
		log.Fatalf("failed to open image: %v", err)
	}

	// Crop the original image to 300x300px size using the center anchor.
	src = imaging.CropAnchor(src, 300, 300, imaging.Center)

	// Resize the cropped image to width = 200px preserving the aspect ratio.
	src = imaging.Resize(src, 200, 0, imaging.Lanczos)

	// Create a blurred version of the image.
	img1 := imaging.Blur(src, 5)

	// Create a grayscale version of the image with higher contrast and sharpness.
	img2 := imaging.Grayscale(src)
	img2 = imaging.AdjustContrast(img2, 20)
	img2 = imaging.Sharpen(img2, 2)

	// Create an inverted version of the image.
	img3 := imaging.Invert(src)

	// Create an embossed version of the image using a convolution filter.
	img4 := imaging.Convolve3x3(
		src,
		[9]float64{
			-1, -1, 0,
			-1, 1, 1,
			0, 1, 1,
		},
		nil,
	)

	// Create a new image and paste the four produced images into it.
	dst := imaging.New(400, 400, color.NRGBA{0, 0, 0, 0})
	dst = imaging.Paste(dst, img1, image.Pt(0, 0))
	dst = imaging.Paste(dst, img2, image.Pt(0, 200))
	dst = imaging.Paste(dst, img3, image.Pt(200, 0))
	dst = imaging.Paste(dst, img4, image.Pt(200, 200))

	// Save the resulting image as JPEG.
	err = imaging.Save(dst, "testdata/out_example.jpg")
	if err != nil {
		log.Fatalf("failed to save image: %v", err)
	}
}

Index

Examples

Constants

View Source
const (
	NoTransform         = types.NoTransform
	FlipHTransform      = types.FlipHTransform
	FlipVTransform      = types.FlipVTransform
	Rotate90Transform   = types.Rotate90Transform
	Rotate180Transform  = types.Rotate180Transform
	Rotate270Transform  = types.Rotate270Transform
	TransverseTransform = types.TransverseTransform
	TransposeTransform  = types.TransposeTransform
)
View Source
const (
	UNKNOWN = types.UNKNOWN
	JPEG    = types.JPEG
	PNG     = types.PNG
	GIF     = types.GIF
	TIFF    = types.TIFF
	WEBP    = types.WEBP
	BMP     = types.BMP
	PBM     = types.PBM
	PGM     = types.PGM
	PPM     = types.PPM
	PAM     = types.PAM
)

Variables

View Source
var ErrUnsupportedFormat = errors.New("imaging: unsupported image format")

ErrUnsupportedFormat means the given image format is not supported.

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 AdjustHue

func AdjustHue(img image.Image, shift float64) *image.NRGBA

AdjustHue changes the hue of the image using the shift parameter (measured in degrees) and returns the adjusted image. The shift = 0 (or 360 / -360 / etc.) gives the original image. The shift = 180 (or -180) corresponds to a 180° degree rotation of the color wheel and thus gives the image with its hue inverted for each pixel.

Examples:

dstImage = imaging.AdjustHue(srcImage, 90) // Shift Hue by 90°.
dstImage = imaging.AdjustHue(srcImage, -30) // Shift Hue by -30°.

func AdjustSaturation

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

AdjustSaturation changes the saturation of the image using the percentage parameter and returns the adjusted image. The percentage must be in the range (-100, 100). The percentage = 0 gives the original image. The percentage = 100 gives the image with the saturation value doubled for each pixel. The percentage = -100 gives the image with the saturation value zeroed for each pixel (grayscale).

Examples:

dstImage = imaging.AdjustSaturation(srcImage, 25) // Increase image saturation by 25%.
dstImage = imaging.AdjustSaturation(srcImage, -10) // Decrease image saturation by 10%.

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 AsNRGBA added in v1.8.2

func AsNRGBA(src image.Image) *image.NRGBA

func AsRGBAData8 added in v1.8.12

func AsRGBAData8(img image.Image) (pix []uint8)

Return contiguous non-premultiplied RGBA pixel data for this image with 8 bits per channel

func AsRGBData8 added in v1.8.8

func AsRGBData8(img image.Image) (pix []uint8)

Return contiguous non-premultiplied RGB pixel data for this image with 8 bits per channel

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.

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 ClonePreservingOrigin added in v1.8.5

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

func ClonePreservingType added in v1.8.2

func ClonePreservingType(src image.Image) image.Image

Clone an image preserving it's type for all known image types or returning an NRGBA64 image otherwise

func ConvertToSRGB added in v1.8.2

func ConvertToSRGB(p *icc.Profile, intent icc.RenderingIntent, use_blackpoint_compensation bool, image_any image.Image) (ans image.Image, err error)

Convert to SRGB based on the supplied ICC color profile. The result may be either the original image unmodified if no color conversion was needed, the original image modified, or a new image (when the original image is not in a supported format).

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.Image

Fill creates an image with the specified dimensions and fills it with the scaled source image. To achieve the correct aspect ratio without stretching, the source image will be cropped.

Example:

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

func Fit

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

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

Example:

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

func FlipH

func FlipH(img image.Image) (ans image.Image)

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

func FlipV

func FlipV(img image.Image) (ans image.Image)

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

func Grayscale

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

Grayscale produces a grayscale version of the image.

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 IsOpaque added in v1.8.2

func IsOpaque(img image.Image) (ans bool)

func IsOpaqueType added in v1.8.13

func IsOpaqueType(img image.Image) (ans bool)

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 NewDecodeConfig added in v1.8.6

func NewDecodeConfig(opts ...DecodeOption) (cfg *decodeConfig)

func NormalizeOrigin added in v1.8.3

func NormalizeOrigin(src image.Image) image.Image

Ensure image has origin at (0, 0). Note that this destroys the original image and returns a new image with the same data, but origin shifted.

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")

func OpenConfig added in v1.7.0

func OpenConfig(filename string) (ans image.Config, format_name string, err error)

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.

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 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 PasteOntoBackground added in v1.8.6

func PasteOntoBackground(img image.Image, bg color.Color) image.Image

Paste the image onto the specified background color.

func Resize

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

func ResizeWithOpacity added in v1.8.13

func ResizeWithOpacity(img image.Image, width, height int, filter ResampleFilter, is_opaque bool) image.Image

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. When is_opaque is true, returns a nrgb.Image otherwise an image.NRGBA. When the image size is unchanged returns a clone with the same image type.

Example:

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

func Rotate

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

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) (ans image.Image)

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

func Rotate270

func Rotate270(img image.Image) (ans image.Image)

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

func Rotate90

func Rotate90(img image.Image) (ans image.Image)

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

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 SetMaxProcs

func SetMaxProcs(value int)

SetMaxProcs limits the number of concurrent processing goroutines to the given value. A value <= 0 clears the limit.

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.

Example:

dstImage := imaging.Sharpen(srcImage, 3.5)

func Thumbnail

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

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.

Example:

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

func Transpose

func Transpose(img image.Image) (ans image.Image)

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

func Transverse

func Transverse(img image.Image) (ans image.Image)

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 Backend added in v1.8.6

type Backend int
const (
	GO_IMAGE Backend = iota
	MAGICK_IMAGE
)

func (Backend) String added in v1.8.6

func (b Backend) String() string

type ColorSpaceType added in v1.8.2

type ColorSpaceType int
const (
	NO_CHANGE_OF_COLORSPACE ColorSpaceType = iota
	SRGB_COLORSPACE
)

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 enabled.

func Backends added in v1.8.6

func Backends(backends ...Backend) DecodeOption

Specify which backends to use to try to load the image, successively. If no backends are specified, the default set are used.

func Background added in v1.8.6

func Background(bg color.Color) DecodeOption

Specify a background color onto which the image should be composed when loading it

func BlackpointCompensation added in v1.8.6

func BlackpointCompensation(enable bool) DecodeOption

Set whether to use blackpoint compensation during ICC profile color conversions

func ColorSpace added in v1.8.2

func ColorSpace(cs ColorSpaceType) DecodeOption

ColorSpace returns a DecodeOption that sets the colorspace that the opened image will be in. Defaults to sRGB. If the image has an embedded ICC color profile it is automatically used to convert colors to sRGB if needed.

func RenderingIntent added in v1.8.6

func RenderingIntent(intent icc.RenderingIntent) DecodeOption

Set the rendering intent to use for ICC profile based color conversions

func ResizeCallback added in v1.8.6

func ResizeCallback(f ResizeCallbackFunction) DecodeOption

Specify a callback function to decide a new size for the image when loading it

func Transform added in v1.8.6

func Transform(t types.TransformType) DecodeOption

Specify a transform to perform on the image when loading it

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 = types.Format

func FormatFromExtension

func FormatFromExtension(ext string) (Format, error)

FormatFromExtension parses image format from filename 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: "jpg" (or "jpeg"), "png", "gif", "tif" (or "tiff") and "bmp" are supported.

type Frame added in v1.8.2

type Frame struct {
	Number      uint          // a 1-based frame number
	TopLeft     image.Point   // location of top-left of this frame w.r.t top left of first frame
	Image       image.Image   `json:"-"` // the actual pixel data
	Delay       time.Duration // the time for which this frame should be visible
	ComposeOnto uint          // the frame number of the frame this frame should be composed onto. 0 means compose onto blank
	Replace     bool          // Do a simple pixel replacement rather than a full alpha blend when compositing this frame
}

func (*Frame) At added in v1.8.4

func (f *Frame) At(x, y int) color.Color

func (*Frame) Bounds added in v1.8.4

func (f *Frame) Bounds() image.Rectangle

func (*Frame) ColorModel added in v1.8.4

func (f *Frame) ColorModel() color.Model

func (*Frame) Dx added in v1.8.5

func (f *Frame) Dx() int

func (*Frame) Dy added in v1.8.5

func (f *Frame) Dy() int

type Image added in v1.8.2

type Image struct {
	Frames       []*Frame    // the actual frames of image data. The first frame is guaranteed to be the size of the image.
	Metadata     *meta.Data  // image metadata
	LoopCount    uint        // 0 means loop forever, 1 means loop once, ...
	DefaultImage image.Image `json:"-"` // a "default image" for an animation that is not part of the actual animation
}

func DecodeAll added in v1.8.2

func DecodeAll(r io.Reader, opts ...DecodeOption) (ans *Image, s io.Reader, err error)

Decode image from r including all animation frames if its an animated image. Returns nil with no error when no supported image is found in r. Also returns a reader that will yield all bytes from r so that this API does not exhaust r.

func OpenAll added in v1.8.2

func OpenAll(filename string, opts ...DecodeOption) (*Image, error)

func (*Image) Bounds added in v1.8.5

func (self *Image) Bounds() image.Rectangle

func (*Image) Clone added in v1.8.3

func (self *Image) Clone() *Image

Create a clone of this image, all data is copied

func (*Image) Coalesce added in v1.8.3

func (self *Image) Coalesce()

Coalesce all animation frames so that each frame is a snapshot of the animation at that instant.

func (*Image) EncodeAsPNG added in v1.8.3

func (self *Image) EncodeAsPNG(w io.Writer) error

Encode this image into a PNG

func (*Image) FlipH added in v1.8.5

func (self *Image) FlipH()

Flip all frames horizontally

func (*Image) FlipV added in v1.8.5

func (self *Image) FlipV()

Flip all frames vertically

func (*Image) PasteOntoBackground added in v1.8.6

func (img *Image) PasteOntoBackground(bg color.Color)

Paste all frames onto the specified background color (OVER alpha blend)

func (*Image) Resize added in v1.8.5

func (self *Image) Resize(width, height int, filter ResampleFilter)

Resize all frames to the specified size

func (*Image) Rotate180 added in v1.8.5

func (self *Image) Rotate180()

Rotate all frames by 180 counter clockwise

func (*Image) Rotate270 added in v1.8.5

func (self *Image) Rotate270()

Rotate all frames by 270 counter clockwise

func (*Image) Rotate90 added in v1.8.5

func (self *Image) Rotate90()

Rotate all frames by 90 counter clockwise

func (*Image) SaveAsPNG added in v1.8.6

func (self *Image) SaveAsPNG(path string, mode fs.FileMode) error

Save this image as PNG

func (*Image) SingleFrame added in v1.8.6

func (ans *Image) SingleFrame() image.Image

func (*Image) Transform added in v1.8.6

func (img *Image) Transform(t types.TransformType)

func (*Image) Transpose added in v1.8.5

func (self *Image) Transpose()

Transpose all frames (flip and rotate 90)

func (*Image) Transverse added in v1.8.5

func (self *Image) Transverse()

Transverse all frames (flip and rotate 90)

type NRGB added in v1.7.0

type NRGB = nrgb.Image

func AsNRGB added in v1.8.6

func AsNRGB(src image.Image) *NRGB

type NRGBColor added in v1.7.0

type NRGBColor = nrgb.Color

type ResampleFilter

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

ResampleFilter specifies a resampling filter to be used for image resizing.

General filter recommendations:

- Lanczos
	A high-quality resampling filter for photographic images yielding sharp results.

- CatmullRom
	A sharp cubic filter that is faster than Lanczos filter while providing similar results.

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

- Linear
	Bilinear resampling filter, produces a smooth output. 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).

type ResizeCallbackFunction added in v1.8.6

type ResizeCallbackFunction func(w, h int) (nw, nh int)

type Scanner added in v1.7.1

type Scanner = types.Scanner

func ScannerForImage added in v1.8.2

func ScannerForImage(img image.Image) Scanner

Directories

Path Synopsis
Package apng implements an APNG image decoder.
Package apng implements an APNG image decoder.
cmd
demo command
frames command
Package jpeg implements a JPEG image decoder and encoder.
Package jpeg implements a JPEG image decoder and encoder.
Package prism provides a set of tools for colour management and conversion.
Package prism provides a set of tools for colour management and conversion.
meta
Package meta and its subpackages provide support for embedded image metadata.
Package meta and its subpackages provide support for embedded image metadata.
meta/autometa
Package autometa provides support for embedded metadata and automatic detection of image formats.
Package autometa provides support for embedded metadata and automatic detection of image formats.
meta/icc
Package icc provides support for working with ICC colour profile data.
Package icc provides support for working with ICC colour profile data.
meta/jpegmeta
Package jpegmeta provides support for working with embedded JPEG metadata.
Package jpegmeta provides support for working with embedded JPEG metadata.
meta/pngmeta
Package pngmeta provides support for working with embedded PNG metadata.
Package pngmeta provides support for working with embedded PNG metadata.
meta/webpmeta
Package webpmeta provides support for working with embedded WebP metadata.
Package webpmeta provides support for working with embedded WebP metadata.

Jump to

Keyboard shortcuts

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