Open Source High-Level Image Processing Go library
Go API That allows Images Rotation, Fit Images, Image Thumbnails Creation, Zoom the Image, Embed or Extend an Image, Add Blur Effects to an Image, and Much More.
What is bimg?
The bimg library is an open-source, high-level image processing solution for Go that enables efficient image reading and manipulation with minimal code. This compact library delivers exceptionally fast performance and well-organized results, requiring significantly less memory than alternative image processing libraries. Built atop the powerful C library libvips, bimg provides robust support for popular image file formats including JPEG, PNG, WEBP, TIFF, PDF, GIF, and SVG. It also facilitates straightforward exporting to JPEG, PNG, and WEBP formats, including support for transparent image outputs.
This versatile library encompasses a comprehensive suite of image processing features designed for developers. Key functionalities include resizing, enlarging, and cropping images—with smart crop support—alongside capabilities to flip, rotate, and create thumbnails. Additional features such as image zooming, watermark addition, Gaussian blur effects, area extraction, image trimming, and format conversion ensure bimg meets diverse development needs for efficient and advanced image manipulation in Go applications.
Getting Started with bimg
The easiest and recommend way to install bimg is via GitHub.
Install bimg via GitHub
go get -u gopkg.in/h2non/bimg.v1Image Conversion to Other Formats via Go
The open source bimg library enables software developers to programmatically convert images to other supported file formats with just a couple of lines of Go code. Let’s suppose you have an in JPEG format and you want to convert it to PNG. You need to provide image information like image name, address, and conversion format. The image will be successfully converted to the suggested format with ease.
How to Convert JPG Image to PNG via Go Library?
buffer, err := bimg.Read("image.jpg")
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
newImage, err := bimg.NewImage(buffer).Convert(bimg.PNG)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
if bimg.NewImage(newImage).Type() == "png" {
fmt.Fprintln(os.Stderr, "The image was converted into png")
}
Crop or Resize Images via Go
The free bimg library gives software developers the capability to resize as well as crop their images inside their own Go applications. You need to provide the width and height of the new image and location as well. It also supports forcing resize operations without preserving the aspect ratio. You can also add text and extract the area of your choice from an image. Crop crops the image to the exact size specified.
How to Resize Image via Go Library?
buffer, err := bimg.Read("image.jpg")
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
newImage, err := bimg.NewImage(buffer).Resize(800, 600)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
size, err := bimg.NewImage(newImage).Size()
if size.Width == 800 && size.Height == 600 {
fmt.Println("The image size is valid")
}
bimg.Write("new.jpg", newImage)
Adding Watermarks to Images
The bimg API has included functionality for adding watermarks to images inside Go applications with ease. Adding a Watermark is a very useful feature for protecting your images. Watermarking is the easiest and very useful way for protecting your photos on the internet. You can easily place a logo or text watermark. The library helps you to programmatically add a watermark to your image on any selected position as well as the opacity.
How to Add Watermark To Image via Go API?
buffer, err := bimg.Read("image.jpg")
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
watermark := bimg.Watermark{
Text: "Chuck Norris (c) 2315",
Opacity: 0.25,
Width: 200,
DPI: 100,
Margin: 150,
Font: "sans bold 12",
Background: bimg.Color{255, 255, 255},
}
newImage, err := bimg.NewImage(buffer).Watermark(watermark)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
bimg.Write("new.jpg", newImage)
Flip and Rotate Images via Go
The open source bimg library gives software developers the capability to rotate their images according to their needs using Go language commands. There is a method for automatic image rotation. The AutoRotate function automatically rotates the image with no additional transformation based on the EXIF orientation metadata, if available. It also provides support for flip or flop images, image Interpretation, image length, image metadata, and many more.
How to Rotate Image inside Go Apps?
buffer, err := bimg.Read("image.jpg")
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
newImage, err := bimg.NewImage(buffer).Rotate(90)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
bimg.Write("new.jpg", newImage)