.. | ||
.travis.yml | ||
adjust.go | ||
convolution.go | ||
doc.go | ||
effects.go | ||
go.mod | ||
go.sum | ||
histogram.go | ||
io.go | ||
LICENSE | ||
README.md | ||
resize.go | ||
scanner.go | ||
tools.go | ||
transform.go | ||
utils.go |
Imaging
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).
Installation
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.
:= imaging.Resize(srcImage, 128, 128, imaging.Lanczos)
dstImage128
// Resize srcImage to width = 800px preserving the aspect ratio.
:= imaging.Resize(srcImage, 800, 0, imaging.Lanczos)
dstImage800
// Scale down srcImage to fit the 800x600px bounding box.
:= imaging.Fit(srcImage, 800, 600, imaging.Lanczos)
dstImageFit
// Resize and crop the srcImage to fill the 100x100px area.
:= imaging.Fill(srcImage, 100, 100, imaging.Center, imaging.Lanczos) dstImageFill
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:
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 |
|
imaging.Linear |
|
imaging.CatmullRom |
|
imaging.Lanczos |
Gaussian Blur
:= imaging.Blur(srcImage, 0.5) dstImage
Sigma parameter allows to control the strength of the blurring effect.
Original image | Sigma = 0.5 | Sigma = 1.5 |
---|---|---|
Sharpening
:= imaging.Sharpen(srcImage, 0.5) dstImage
Sharpen
uses gaussian function internally. Sigma
parameter allows to control the strength of the sharpening effect.
Original image | Sigma = 0.5 | Sigma = 1.5 |
---|---|---|
Gamma correction
:= imaging.AdjustGamma(srcImage, 0.75) dstImage
Original image | Gamma = 0.75 | Gamma = 1.25 |
---|---|---|
Contrast adjustment
:= imaging.AdjustContrast(srcImage, 20) dstImage
Original image | Contrast = 15 | Contrast = -15 |
---|---|---|
Brightness adjustment
:= imaging.AdjustBrightness(srcImage, 20) dstImage
Original image | Brightness = 10 | Brightness = -10 |
---|---|---|
Saturation adjustment
:= imaging.AdjustSaturation(srcImage, 20) dstImage
Original image | Saturation = 30 | Saturation = -30 |
---|---|---|
FAQ
Incorrect image orientation after processing (e.g. an image appears rotated after resizing)
Most probably, the given image contains the EXIF orientation tag. The
stadard image/*
packages do not support loading and saving
this kind of information. To fix the issue, try opening images with the
AutoOrientation
decode option. If this option is set to
true
, the image orientation is changed after decoding,
according to the orientation tag (if present). Here’s the example:
, err := imaging.Open("test.jpg", imaging.AutoOrientation(true)) img
What’s
the difference between imaging
and gift
packages?
imaging is
designed to be a lightweight and simple image manipulation package. It
provides basic image processing functions and a few helper functions
such as Open
and Save
. It consistently returns
*image.NRGBA image type (8 bits per channel, RGBA).
gift supports more advanced image processing, for example, sRGB/Linear color space conversions. It also supports different output image types (e.g. 16 bits per channel) and provides easy-to-use API for chaining multiple processing steps together.
Example code
package main
import (
"image"
"image/color"
"log"
"github.com/disintegration/imaging"
)
func main() {
// Open a test image.
, err := imaging.Open("testdata/flowers.png")
srcif err != nil {
.Fatalf("failed to open image: %v", err)
log}
// Crop the original image to 300x300px size using the center anchor.
= imaging.CropAnchor(src, 300, 300, imaging.Center)
src
// Resize the cropped image to width = 200px preserving the aspect ratio.
= imaging.Resize(src, 200, 0, imaging.Lanczos)
src
// Create a blurred version of the image.
:= imaging.Blur(src, 5)
img1
// Create a grayscale version of the image with higher contrast and sharpness.
:= imaging.Grayscale(src)
img2 = imaging.AdjustContrast(img2, 20)
img2 = imaging.Sharpen(img2, 2)
img2
// Create an inverted version of the image.
:= imaging.Invert(src)
img3
// Create an embossed version of the image using a convolution filter.
:= imaging.Convolve3x3(
img4 ,
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.
:= 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))
dst
// Save the resulting image as JPEG.
= imaging.Save(dst, "testdata/out_example.jpg")
err if err != nil {
.Fatalf("failed to save image: %v", err)
log}
}
Output: