Socket
Socket
Sign inDemoInstall

github.com/mantyr/imaging

Package Overview
Dependencies
0
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    github.com/mantyr/imaging

Package imaging provides basic image manipulation functions (resize, rotate, flip, crop, etc.). This package is based on the standard Go image package and works best along with it. Image manipulation functions provided by the package take 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). Imaging package uses parallel goroutines for faster image processing. To achieve maximum performance, make sure to allow Go to utilize all CPU cores:


Version published

Readme

Source

Imaging

Package imaging provides basic image manipulation functions (resize, rotate, flip, crop, etc.). This package is based on the standard Go image package and works best along with it.

Image manipulation functions provided by the package take 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).

Installation

Imaging requires Go version 1.2 or greater.

go get -u github.com/disintegration/imaging

Documentation

http://godoc.org/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

// 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 make a 100x100px thumbnail
dstImageThumb := imaging.Thumbnail(srcImage, 100, 100, 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, but it's 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. Will be resized from 512x512px to 128x128px.

srcImage

FilterResize result
imaging.NearestNeighbordstImage
imaging.BoxdstImage
imaging.LineardstImage
imaging.MitchellNetravalidstImage
imaging.CatmullRomdstImage
imaging.GaussiandstImage
imaging.LanczosdstImage

Gaussian Blur

dstImage := imaging.Blur(srcImage, 0.5)

Sigma parameter allows to control the strength of the blurring effect.

Original imageSigma = 0.5Sigma = 1.5
srcImagedstImagedstImage

Sharpening

dstImage := imaging.Sharpen(srcImage, 0.5)

Uses gaussian function internally. Sigma parameter allows to control the strength of the sharpening effect.

Original imageSigma = 0.5Sigma = 1.5
srcImagedstImagedstImage

Gamma correction

dstImage := imaging.AdjustGamma(srcImage, 0.75)
Original imageGamma = 0.75Gamma = 1.25
srcImagedstImagedstImage

Contrast adjustment

dstImage := imaging.AdjustContrast(srcImage, 20)
Original imageContrast = 20Contrast = -20
srcImagedstImagedstImage

Brightness adjustment

dstImage := imaging.AdjustBrightness(srcImage, 20)
Original imageBrightness = 20Brightness = -20
srcImagedstImagedstImage

Complete code example

Here is the code example that loads several images, makes thumbnails of them and combines them together side-by-side.

package main

import (
    "image"
    "image/color"
    "runtime"
    
    "github.com/disintegration/imaging"
)

func main() {
    // use all CPU cores for maximum performance
    runtime.GOMAXPROCS(runtime.NumCPU())

    // input files
    files := []string{"01.jpg", "02.jpg", "03.jpg"}

    // load images and make 100x100 thumbnails of them
    var thumbnails []image.Image
    for _, file := range files {
        img, err := imaging.Open(file)
        if err != nil {
            panic(err)
        }
        thumb := imaging.Thumbnail(img, 100, 100, imaging.CatmullRom)
        thumbnails = append(thumbnails, thumb)
    }

    // create a new blank image
    dst := imaging.New(100*len(thumbnails), 100, color.NRGBA{0, 0, 0, 0})

    // paste thumbnails into the new image side by side
    for i, thumb := range thumbnails {
        dst = imaging.Paste(dst, thumb, image.Pt(i*100, 0))
    }

    // save the combined image to file
    err := imaging.Save(dst, "dst.jpg")
    if err != nil {
        panic(err)
    }
}

FAQs

Last updated on 19 Mar 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc