Open Source Go Library for Image Creation & Manipulation
Powerful Go API that supports Resize, Crop & Rotate JPEG, PNG, GIF, TIFF, and BMP Images. You can also adjust image brightness, contrast, gamma correction of the images.
The open source Go Imaging API is a very powerful package that provides complete functionality related to image creation and manipulation. The library is very flexible and allows developers to easily create new images and modify existing ones with just a couple of lines of code.
The Imaging is a pure Go library that is small in size and efficient in functionality. It has included support for several important image file formats like JPEG, PNG, GIF, TIFF, BMP, and many more. The library has also included several resampling filters for image resizing. Some important filters are NearestNeighbor, Lanczos, CatmullRom, MitchellNetravali, Linear, Box, and so on. The library has also facilitated developers to create custom filters.
The free Imaging library has included several important features related to image processing, such as resizing images, image rotation, image cropping, adjusting image brightness, image contrast adjustments, gamma correction of the images, changing the saturation of the image, clone images, image blur, encode and decode images, image overlay, add sharpness, create thumbnail and many other features.
Getting Started with Imaging
The easiest and recommended way to install Imagingis via GitHub.
Install Imaging via GitHub
go get -u github.com/disintegration/imaging
Generate New Image via Go API
The open source imaging library has included support for generating new images inside their own application using Go commands. The new image creation requires image width, height, background color of the image, and output format of the image. You can also easily modify the created image and perform different operations like flip, setting opacity, blend, blur, and much more.
Generate New Image via Go API
func New(width, height int, fillColor color.Color) *image.NRGBA {
if width <= 0 || height <= 0 {
return &image.NRGBA{}
}
c := color.NRGBAModel.Convert(fillColor).(color.NRGBA)
if (c == color.NRGBA{0, 0, 0, 0}) {
return image.NewNRGBA(image.Rect(0, 0, width, height))
}
return &image.NRGBA{
Pix: bytes.Repeat([]byte{c.R, c.G, c.B, c.A}, width*height),
Stride: 4 * width,
Rect: image.Rect(0, 0, width, height),
}
}
Image Conversion to Other Formats via Go
Software developers can easily convert their images to other supported file formats inside their own GO Apps using a free imaging library. You just need to provide the image name and the output image format. Using the Save function you can easily export the images to several other supported image file formats such as PNG, BMP, GIF, JPEG, TIFF, and more.
Resize and Crop Images
The Free imaging API has included functionality for resizing images according to your needs using Go commands. First, you need to open an image and provide the height as well as the width of the image to resize it. Another option is that you can resize the image by just providing width by preserving the aspect ratio. The library also allows cropping of the original image by providing custom width, height, and using the center anchor.
Crop & Resize Image via Go API
func cropAndResize(img image.Image, width, height int, anchor Anchor, filter ResampleFilter) *image.NRGBA {
dstW, dstH := width, height
srcBounds := img.Bounds()
srcW := srcBounds.Dx()
srcH := srcBounds.Dy()
srcAspectRatio := float64(srcW) / float64(srcH)
dstAspectRatio := float64(dstW) / float64(dstH)
var tmp *image.NRGBA
if srcAspectRatio < dstAspectRatio {
cropH := float64(srcW) * float64(dstH) / float64(dstW)
tmp = CropAnchor(img, srcW, int(math.Max(1, cropH)+0.5), anchor)
} else {
cropW := float64(srcH) * float64(dstW) / float64(dstH)
tmp = CropAnchor(img, int(math.Max(1, cropW)+0.5), srcH, anchor)
}
return Resize(tmp, dstW, dstH, filter)
}
Flip, Rotate, Blur & Clone Images
The Imaging library has included several important features for image manipulation such as image flipping, image rotation, blurring, and cloning. To make a copy of an existing image you just need to call the Clone function and provide the existing image. The library also supports rotating and flipping your image with just a couple of lines of code. You can easily rotate an image by the given angle counter-clockwise. The angle parameter is the rotation angle in degrees.
Clone Image via Go API
func Clone(img image.Image) *image.NRGBA {
src := newScanner(img)
dst := image.NewNRGBA(image.Rect(0, 0, src.w, src.h))
size := src.w * 4
parallel(0, src.h, func(ys <-chan int) {
for y := range ys {
i := y * dst.Stride
src.scan(0, y, src.w, y+1, dst.Pix[i:i+size])
}
})
return dst
}