resize

package module
v0.0.0-...-4d25061 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2013 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"

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.Resize(width, height 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

Types

type Filter

type Filter interface {
	Interpolate(x, y float32) color.RGBA64
}

Filter can interpolate at points (x,y)

func Bicubic

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

Bicubic interpolation (with cubic hermite spline)

func Bilinear

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

Bilinear interpolation

func Lanczos2

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

Lanczos interpolation (a=2)

func Lanczos2Lut

func Lanczos2Lut(img image.Image, factor [2]float32) Filter

Lanczos interpolation (a=2) using a look-up table to speed up computation

func Lanczos3

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

Lanczos interpolation (a=3)

func Lanczos3Lut

func Lanczos3Lut(img image.Image, factor [2]float32) Filter

Lanczos interpolation (a=3) using a look-up table to speed up computation

func MitchellNetravali

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

Mitchell-Netravali interpolation

func NearestNeighbor

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

Nearest-neighbor interpolation

type InterpolationFunction

type InterpolationFunction func(image.Image, [2]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.

type Trans2

type Trans2 [6]float32

Trans2 is a 2-dimensional linear transformation.

func (*Trans2) Eval

func (t *Trans2) Eval(x, y float32) (u, v float32)

Apply the transformation to a point (x,y).

Jump to

Keyboard shortcuts

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