disintegration /imaging
Imaging is a simple image processing package for Go
★ 5,767⑂ 485👁 74⑃ 47● 134▤ 7
+0
日增长
5,767
stars
485
forks
74
watchers
47
prs
134
issues
7
releases
README
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
https://pkg.go.dev/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
go
// 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:
- 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
go
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
Top issues
#143Add support for opening remote images4 comments · 2021-06-25#167Snyk reports vulnerability3 comments · 2023-11-02#151how can we resize image3 comments · 2021-10-23#146WebP support2 comments · 2021-07-30#94can you put more example3 comments · 2019-04-17#179CVE-2023-36308 - Improper Input Validation vulnerability in github.com/disintegration/imaging2 comments · 2026-02-23#173Can't make GIFs work properly2 comments · 2024-11-09#150How to get file from postman2 comments · 2021-10-12
Latest release
v1.6.2 v1.6.2 2019-11-16Update golang.org/x/image dependency (improves BMP and TIFF support).