You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.7 KiB

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