imaging

package module
v0.0.0-...-73d266f Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2013 License: MIT Imports: 11 Imported by: 0

README

Imaging

Package imaging provides basic image manipulation functions (resize, rotate, flip, crop, etc.) as well as simplified image loading and saving. This package is based on the standard Go image package. All the image manipulation functions provided by the package take any image type that implements image.Image interface, and return a new image of *image.NRGBA type (32 bit RGBA colors, not premultiplied by alpha).

###Installation

go get github.com/disintegration/imaging
Documentation

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

Usage
package main

import (
    "github.com/disintegration/imaging"
    "image"
    "image/color"
)

func main() {
    src, _ := imaging.Open("src.png") // load an image from file (returns image.Image interface)
    var dst *image.NRGBA
    
    dst = imaging.New(800, 600, color.NRGBA{255, 0, 0, 255}) // create a new 800x600px image filled with red color
    dst = imaging.Clone(src) // make a copy of the image
    
    dst = imaging.Rotate90(src) // rotate 90 degrees clockwise 
    dst = imaging.Rotate180(src) // rotate 180 degrees clockwise
    dst = imaging.Rotate270(src) // rotate 270 degrees clockwise

    dst = imaging.FlipH(src) // flip horizontally (from left to right)
    dst = imaging.FlipV(src) // flip vertically (from top to bottom)

    // Resize, Fit and Thumbnail functions take resampling filter as 4th argument.
    // Supported filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali,
    // CatmullRom, BSpline, Gaussian, Lanczos, Hann, Hamming, Blackman, Bartlett, Welch, Cosine.

    dst = imaging.Resize(src, 600, 400, imaging.CatmullRom) // resize to 600x400 px using CatmullRom cubic filter
    dst = imaging.Resize(src, 600, 0, imaging.CatmullRom) // resize to width = 600, preserve the image aspect ratio
    dst = imaging.Resize(src, 0, 400, imaging.CatmullRom) // resize to height = 400, preserve the image aspect ratio
    
    dst = imaging.Fit(src, 800, 600, imaging.CatmullRom) // scale down the image to fit the given maximum width and height
    dst = imaging.Thumbnail(src, 100, 100, imaging.CatmullRom) // resize and crop the image to make a 100x100 thumbnail
    
    dst = imaging.Crop(src, image.Rect(50, 50, 100, 100)) // cut out a rectangular region from the image
    dst = imaging.CropCenter(src, 200, 100) // cut out a 200x100 px region from the center of the image
    dst = imaging.Paste(dst, src, image.Pt(50, 50)) // paste the src image to the dst image at the given position
    dst = imaging.PasteCenter(dst, src) // paste the src image to the center of the dst image
    
    // draw one image over another at the given position and with the given opacity (from 0.0 to 1.0)
    dst = imaging.Overlay(dst, src, image.Pt(50, 30), 1.0)
    
    imaging.Save(dst, "dst.jpg") // save the image to file
}

Documentation

Overview

Package imaging provides basic image manipulation functions (resize, rotate, flip, crop, etc.) as well as simplified image loading and saving.

This package is based on the standard Go image package. All the image manipulation functions provided by the package take any image type that implements image.Image interface, and return a new image of *image.NRGBA type (32 bit RGBA colors, not premultiplied by alpha).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clone

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

Clone returns a copy of the img. New image bounds will be (0, 0)-(width, height).

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 CropCenter

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

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

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 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 Open

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

Open loads an image from file

func Overlay

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

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

Usage examples:

// draw the sprite over the background at position (50, 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 Paste

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

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

func PasteCenter

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

Paste pastes the src image to the center of the img image and returns the combined image.

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 Rotate180

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

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

func Rotate270

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

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

func Rotate90

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

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

func Save

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

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

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.Fit(srcImage, 100, 100, imaging.Lanczos)

Types

type ResampleFilter

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

Resample filter struct. It can be used to make custom filters.

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

General filter recommendations:

- Lanczos
	Probably the best resampling filter for photographic images yielding sharp results,
	but 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 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 resampling filter appropriate for downscaling.
	When upscaling it's similar to NearestNeighbor.

- NearestNeighbor
	Fastest resample filter, no antialiasing at all. Rarely used.
var BSpline ResampleFilter

Cubic B-spline - smooth cubic filter (BC-spline; B=1; C=0).

var Bartlett ResampleFilter

Bartlett-windowed sinc filter (3 lobes).

var Blackman ResampleFilter

Blackman-windowed sinc filter (3 lobes).

var Box ResampleFilter

Box filter (averaging pixels).

var CatmullRom ResampleFilter

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

var Cosine ResampleFilter

Cosine-windowed sinc filter (3 lobes).

var Gaussian ResampleFilter

Gaussian Blurring Filter.

var Hamming ResampleFilter

Hamming-windowed sinc filter (3 lobes).

var Hann ResampleFilter

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

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

var NearestNeighbor ResampleFilter

Nearest-neighbor filter, no anti-aliasing.

var Welch ResampleFilter

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