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.
bimg is an open source high-level image processing Go library that provides the capability for reading and manipulating images with just a couple of lines of Go code. The library is smaller in size but produces very well-organized and efficient results. The library is very fast compared to other available libraries that require very small memory to complete the task.
The bimg is a high-level C library that is built on top of the libvips which is a very powerful library for processing images. It provides support for reading some very popular image file formats like JPEG, PNG, WEBP, TIFF, PDF, GIF, SVG, etc. You can also easily export images to JPEG, PNG, WEBP formats as well as to transparent images.
The bimg library has included support for several important image processing features such as resizing images, enlarging the image, image cropping including smart crop support, flip or rotate images, creating image thumbnails, image zooming support, adding watermark and Gaussian blur effect, extract specific area from an image, trim images, image conversion to other formats and many more.
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.v1
Image 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.
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
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.
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.
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
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.
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)