封装成像提供基本的图像处理功能(调整大小、旋转、裁剪、亮度/对比度调整等)。

包提供的所有图像处理函数都接受任何实现接口的图像类型 作为输入,并返回类型的新图像(32 位 RGBA 颜色,非预乘 alpha).image.Image*image.NRGBA

Grigory Dryapak fc05e1fd57 transform: omit unnecessary conversion 8 years ago
testdata 639cb399cc doc: add example 8 years ago
.travis.yml c3956f26e8 travis ci: update go versions 8 years ago
LICENSE b7f3229c68 license: 2017 8 years ago
README.md 6438d817e8 readme: use images from testdata 8 years ago
adjust.go 01751cef8a consistency of indentation in comments 8 years ago
adjust_test.go 64e14965bc tests: add testdata & golden tests 8 years ago
clone.go 38fb9473ae clone: adjust YCbCr to RGB conversion 8 years ago
clone_test.go 0198c96e30 move Clone to separate file 8 years ago
convolution.go 90156c85f3 add convolution functions 8 years ago
convolution_test.go 64e14965bc tests: add testdata & golden tests 8 years ago
effects.go 01751cef8a consistency of indentation in comments 8 years ago
effects_test.go ac27d1805a effects: improve Blur & Sharpen perfomance 8 years ago
example_test.go 639cb399cc doc: add example 8 years ago
helpers.go 0198c96e30 move Clone to separate file 8 years ago
helpers_test.go 0198c96e30 move Clone to separate file 8 years ago
histogram.go 0df405384f histogram: omit unnecessary type declaration 8 years ago
histogram_test.go e6400992a4 histogram: fmt 8 years ago
resize.go b085d8f4ea resize: reduce the number of allocations 8 years ago
resize_test.go 64e14965bc tests: add testdata & golden tests 8 years ago
tools.go 01751cef8a consistency of indentation in comments 8 years ago
tools_test.go 64e14965bc tests: add testdata & golden tests 8 years ago
transform.go dd50a3ee99 transform: omit unnecessary conversion 8 years ago
transform_test.go a5858022df transform: optimize arbitrary angle rotation for multiples of 90 degrees 8 years ago
utils.go b82515fd4d utils: clamp optimization 8 years ago
utils_test.go c663879bbf simplify bool comparison 8 years ago

README.md

Imaging

GoDoc Build Status Coverage Status

Package imaging provides basic image manipulation functions (resize, rotate, flip, crop, etc.). This package is based on the standard Go image package and works best along with it.

Image manipulation functions provided by the package take 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).

Installation

Imaging requires Go version 1.2 or greater.

go get -u github.com/disintegration/imaging

Documentation

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

Usage examples

A few usage examples can be found below. See the documentation for the full list of supported functions.

Image resizing

// Resize srcImage to size = 128x128px using the Lanczos filter.
dstImage128 := imaging.Resize(srcImage, 128, 128, imaging.Lanczos)

// Resize srcImage to width = 800px preserving the aspect ratio.
dstImage800 := imaging.Resize(srcImage, 800, 0, imaging.Lanczos)

// Scale down srcImage to fit the 800x600px bounding box.
dstImageFit := imaging.Fit(srcImage, 800, 600, imaging.Lanczos)

// Resize and crop the srcImage to fill the 100x100px area.
dstImageFill := imaging.Fill(srcImage, 100, 100, imaging.Center, imaging.Lanczos)

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

  • NearestNeighbor - Fastest resampling filter, no antialiasing.
  • Box - Simple and fast averaging filter appropriate for downscaling. When upscaling it's similar to NearestNeighbor.
  • Linear - Bilinear filter, smooth and reasonably fast.
  • MitchellNetravali - А smooth bicubic filter.
  • CatmullRom - A sharp bicubic filter.
  • Gaussian - Blurring filter that uses gaussian function, useful for noise removal.
  • Lanczos - High-quality resampling filter for photographic images yielding sharp results, but it's slower than cubic filters.

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

The original image.

srcImage

The same image resized from 512x512px to 128x128px 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 = 10 Contrast = -10
srcImage dstImage dstImage

Brightness adjustment

dstImage := imaging.AdjustBrightness(srcImage, 20)
Original image Brightness = 10 Brightness = -10
srcImage dstImage dstImage

Example code

package main

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

	"github.com/disintegration/imaging"
)

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

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

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

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

	// 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(512, 512, color.NRGBA{0, 0, 0, 0})
	dst = imaging.Paste(dst, img1, image.Pt(0, 0))
	dst = imaging.Paste(dst, img2, image.Pt(0, 256))
	dst = imaging.Paste(dst, img3, image.Pt(256, 0))
	dst = imaging.Paste(dst, img4, image.Pt(256, 256))

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

Output:

dstImage