1# This package is no longer being updated! Please look for alternatives if that bothers you. 2 3Resize 4====== 5 6Image resizing for the [Go programming language](http://golang.org) with common interpolation methods. 7 8[![Build Status](https://travis-ci.org/nfnt/resize.svg)](https://travis-ci.org/nfnt/resize) 9 10Installation 11------------ 12 13```bash 14$ go get github.com/nfnt/resize 15``` 16 17It's that easy! 18 19Usage 20----- 21 22This package needs at least Go 1.1. Import package with 23 24```go 25import "github.com/nfnt/resize" 26``` 27 28The resize package provides 2 functions: 29 30* `resize.Resize` creates a scaled image with new dimensions (`width`, `height`) using the interpolation function `interp`. 31 If either `width` or `height` is set to 0, it will be set to an aspect ratio preserving value. 32* `resize.Thumbnail` downscales an image preserving its aspect ratio to the maximum dimensions (`maxWidth`, `maxHeight`). 33 It will return the original image if original sizes are smaller than the provided dimensions. 34 35```go 36resize.Resize(width, height uint, img image.Image, interp resize.InterpolationFunction) image.Image 37resize.Thumbnail(maxWidth, maxHeight uint, img image.Image, interp resize.InterpolationFunction) image.Image 38``` 39 40The provided interpolation functions are (from fast to slow execution time) 41 42- `NearestNeighbor`: [Nearest-neighbor interpolation](http://en.wikipedia.org/wiki/Nearest-neighbor_interpolation) 43- `Bilinear`: [Bilinear interpolation](http://en.wikipedia.org/wiki/Bilinear_interpolation) 44- `Bicubic`: [Bicubic interpolation](http://en.wikipedia.org/wiki/Bicubic_interpolation) 45- `MitchellNetravali`: [Mitchell-Netravali interpolation](http://dl.acm.org/citation.cfm?id=378514) 46- `Lanczos2`: [Lanczos resampling](http://en.wikipedia.org/wiki/Lanczos_resampling) with a=2 47- `Lanczos3`: [Lanczos resampling](http://en.wikipedia.org/wiki/Lanczos_resampling) with a=3 48 49Which of these methods gives the best results depends on your use case. 50 51Sample usage: 52 53```go 54package main 55 56import ( 57 "github.com/nfnt/resize" 58 "image/jpeg" 59 "log" 60 "os" 61) 62 63func main() { 64 // open "test.jpg" 65 file, err := os.Open("test.jpg") 66 if err != nil { 67 log.Fatal(err) 68 } 69 70 // decode jpeg into image.Image 71 img, err := jpeg.Decode(file) 72 if err != nil { 73 log.Fatal(err) 74 } 75 file.Close() 76 77 // resize to width 1000 using Lanczos resampling 78 // and preserve aspect ratio 79 m := resize.Resize(1000, 0, img, resize.Lanczos3) 80 81 out, err := os.Create("test_resized.jpg") 82 if err != nil { 83 log.Fatal(err) 84 } 85 defer out.Close() 86 87 // write new image to file 88 jpeg.Encode(out, m, nil) 89} 90``` 91 92Caveats 93------- 94 95* Optimized access routines are used for `image.RGBA`, `image.NRGBA`, `image.RGBA64`, `image.NRGBA64`, `image.YCbCr`, `image.Gray`, and `image.Gray16` types. All other image types are accessed in a generic way that will result in slow processing speed. 96* JPEG images are stored in `image.YCbCr`. This image format stores data in a way that will decrease processing speed. A resize may be up to 2 times slower than with `image.RGBA`. 97 98 99Downsizing Samples 100------- 101 102Downsizing is not as simple as it might look like. Images have to be filtered before they are scaled down, otherwise aliasing might occur. 103Filtering is highly subjective: Applying too much will blur the whole image, too little will make aliasing become apparent. 104Resize tries to provide sane defaults that should suffice in most cases. 105 106### Artificial sample 107 108Original image 109![Rings](http://nfnt.github.com/img/rings_lg_orig.png) 110 111<table> 112<tr> 113<th><img src="http://nfnt.github.com/img/rings_300_NearestNeighbor.png" /><br>Nearest-Neighbor</th> 114<th><img src="http://nfnt.github.com/img/rings_300_Bilinear.png" /><br>Bilinear</th> 115</tr> 116<tr> 117<th><img src="http://nfnt.github.com/img/rings_300_Bicubic.png" /><br>Bicubic</th> 118<th><img src="http://nfnt.github.com/img/rings_300_MitchellNetravali.png" /><br>Mitchell-Netravali</th> 119</tr> 120<tr> 121<th><img src="http://nfnt.github.com/img/rings_300_Lanczos2.png" /><br>Lanczos2</th> 122<th><img src="http://nfnt.github.com/img/rings_300_Lanczos3.png" /><br>Lanczos3</th> 123</tr> 124</table> 125 126### Real-Life sample 127 128Original image 129![Original](http://nfnt.github.com/img/IMG_3694_720.jpg) 130 131<table> 132<tr> 133<th><img src="http://nfnt.github.com/img/IMG_3694_300_NearestNeighbor.png" /><br>Nearest-Neighbor</th> 134<th><img src="http://nfnt.github.com/img/IMG_3694_300_Bilinear.png" /><br>Bilinear</th> 135</tr> 136<tr> 137<th><img src="http://nfnt.github.com/img/IMG_3694_300_Bicubic.png" /><br>Bicubic</th> 138<th><img src="http://nfnt.github.com/img/IMG_3694_300_MitchellNetravali.png" /><br>Mitchell-Netravali</th> 139</tr> 140<tr> 141<th><img src="http://nfnt.github.com/img/IMG_3694_300_Lanczos2.png" /><br>Lanczos2</th> 142<th><img src="http://nfnt.github.com/img/IMG_3694_300_Lanczos3.png" /><br>Lanczos3</th> 143</tr> 144</table> 145 146 147License 148------- 149 150Copyright (c) 2012 Jan Schlicht <janschlicht@gmail.com> 151Resize is released under a MIT style license. 152