commit | bbcee2f5c9d5e94ca42c8b50ec847fec64a6c134 | [log] [tgz] |
---|---|---|
author | Grigory Dryapak <grigory.dryapak@gmail.com> | Tue May 08 19:53:14 2018 |
committer | Grigory Dryapak <grigory.dryapak@gmail.com> | Tue May 08 19:53:14 2018 |
tree | bf13706e01403fd895c322f298c5f306d1944abf | |
parent | bbec8e448443b07c0859e094701e1dd814c25216 [diff] |
helpers: fix race condition and simplify New
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, not premultiplied by alpha).
go get -u github.com/disintegration/imaging
http://godoc.org/github.com/disintegration/imaging
A few usage examples can be found below. See the documentation for the full list of supported functions.
// 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, 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
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 |
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 |
---|---|---|
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 |
---|---|---|
dstImage := imaging.AdjustGamma(srcImage, 0.75)
Original image | Gamma = 0.75 | Gamma = 1.25 |
---|---|---|
dstImage := imaging.AdjustContrast(srcImage, 20)
Original image | Contrast = 15 | Contrast = -15 |
---|---|---|