Giter Site home page Giter Site logo

imaging's Introduction

Imaging

GoDoc Build Status Coverage Status

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 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:

  • 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

Filter Resize result
imaging.NearestNeighbor dstImage
imaging.Box dstImage
imaging.Linear dstImage
imaging.MitchellNetravali dstImage
imaging.CatmullRom dstImage
imaging.Gaussian dstImage
imaging.Lanczos dstImage

Resize functions comparison

Original image:

srcImage

Resize the image to width=100px and height=100px:

dstImage := imaging.Resize(srcImage, 100, 100, imaging.Lanczos)

dstImage

Resize the image to width=100px preserving the aspect ratio:

dstImage := imaging.Resize(srcImage, 100, 0, imaging.Lanczos)

dstImage

Resize the image to fit the 100x100px boundng box preserving the aspect ratio:

dstImage := imaging.Fit(srcImage, 100, 100, imaging.Lanczos)

dstImage

Resize and crop the image with a center anchor point to fill the 100x100px area:

dstImage := imaging.Fill(srcImage, 100, 100, imaging.Center, imaging.Lanczos)

dstImage

Gaussian Blur

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

Sharpening

dstImage := imaging.Sharpen(srcImage, 0.5)

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

Original image Sigma = 0.5 Sigma = 1.5
srcImage dstImage dstImage

Gamma correction

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

Contrast adjustment

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

Brightness adjustment

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

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"
    
    "github.com/disintegration/imaging"
)

func main() {

    // 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)
    }
}

imaging's People

Contributors

dherbst avatar disintegration avatar djworth avatar hjr265 avatar morphar avatar richardprior avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.