resize

package module
v0.0.0-...-f3cac2f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2014 License: ISC Imports: 4 Imported by: 0

README

Resize

Image resizing for the Go programming language with common interpolation methods.

Build Status

Installation

$ go get github.com/nfnt/resize

It's that easy!

Usage

Import package with

import "github.com/nfnt/resize"

The resize package provides 2 functions:

  • resize.Resize creates a scaled image with new dimensions (width, height) using the interpolation function interp. If either width or height is set to 0, it will be set to an aspect ratio preserving value.
  • resize.Thumbnail downscales an image preserving its aspect ratio to the maximum dimensions (maxWidth, maxHeight). It will return the original image if original sizes are smaller than the provided dimensions.
resize.Resize(width, height uint, img image.Image, interp resize.InterpolationFunction) image.Image
resize.Thumbnail(maxWidth, maxHeight uint, img image.Image, interp resize.InterpolationFunction) image.Image

The provided interpolation functions are (from fast to slow execution time)

Which of these methods gives the best results depends on your use case.

Sample usage:

package main

import (
	"github.com/nfnt/resize"
	"image/jpeg"
	"log"
	"os"
)

func main() {
	// open "test.jpg"
	file, err := os.Open("test.jpg")
	if err != nil {
		log.Fatal(err)
	}

	// decode jpeg into image.Image
	img, err := jpeg.Decode(file)
	if err != nil {
		log.Fatal(err)
	}
	file.Close()

	// resize to width 1000 using Lanczos resampling
	// and preserve aspect ratio
	m := resize.Resize(1000, 0, img, resize.Lanczos3)

	out, err := os.Create("test_resized.jpg")
	if err != nil {
		log.Fatal(err)
	}
	defer out.Close()

	// write new image to file
	jpeg.Encode(out, m, nil)
}

Downsizing Samples

Downsizing is not as simple as it might look like. Images have to be filtered before they are scaled down, otherwise aliasing might occur. Filtering is highly subjective: Applying too much will blur the whole image, too little will make aliasing become apparent. Resize tries to provide sane defaults that should suffice in most cases.

Artificial sample

Original image Rings


Nearest-Neighbor

Bilinear

Bicubic

Mitchell-Netravali

Lanczos2

Lanczos3
Real-Life sample

Original image
Original


Nearest-Neighbor

Bilinear

Bicubic

Mitchell-Netravali

Lanczos2

Lanczos3

License

Copyright (c) 2012 Jan Schlicht janschlicht@gmail.com Resize is released under a MIT style license.

Documentation

Overview

Package resize implements various image resizing methods.

The package works with the Image interface described in the image package. Various interpolation methods are provided and multiple processors may be utilized in the computations.

Example:

imgResized := resize.Resize(1000, 0, imgOld, resize.MitchellNetravali)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Resize

func Resize(width, height uint, img image.Image, interp InterpolationFunction) image.Image

Resize an image to new width and height using the interpolation function interp. A new image with the given dimensions will be returned. If one of the parameters width or height is set to 0, its size will be calculated so that the aspect ratio is that of the originating image. The resizing algorithm uses channels for parallel computation.

func Sinc

func Sinc(x float64) float64

normalized sinc function

func Sinc1

func Sinc1(x float64) (y float64)

unnormalized sinc function

func Thumbnail

func Thumbnail(maxWidth, maxHeight uint, img image.Image, interp InterpolationFunction) image.Image

Thumbnail will downscale provided image to max width and height preserving original aspect ratio and using the interpolation function interp. It will return original image, without processing it, if original sizes are already smaller than provided constraints.

Types

type Filter

type Filter interface {
	SetKernelWeights(u float32)
	Interpolate(u float32, y int) color.RGBA64
}

Filter can interpolate at points (x,y)

func Bicubic

func Bicubic(img image.Image, factor float32) Filter

Bicubic interpolation (with cubic hermite spline)

func Bilinear

func Bilinear(img image.Image, factor float32) Filter

Bilinear interpolation

func Lanczos2

func Lanczos2(img image.Image, factor float32) Filter

Lanczos interpolation (a=2)

func Lanczos3

func Lanczos3(img image.Image, factor float32) Filter

Lanczos interpolation (a=3)

func MitchellNetravali

func MitchellNetravali(img image.Image, factor float32) Filter

Mitchell-Netravali interpolation

func NearestNeighbor

func NearestNeighbor(img image.Image, factor float32) Filter

Nearest-neighbor interpolation

type InterpolationFunction

type InterpolationFunction func(image.Image, float32) Filter

InterpolationFunction return a Filter implementation that operates on an image. Two factors allow to scale the filter kernels in x- and y-direction to prevent moire patterns.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL