github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/gift/README.md (about) 1 # GO IMAGE FILTERING TOOLKIT (GIFT) 2 3 [](https://godoc.org/github.com/disintegration/gift) 4 [](https://travis-ci.org/disintegration/gift) 5 [](https://coveralls.io/github/disintegration/gift?branch=master) 6 7 *Package gift provides a set of useful image processing filters.* 8 9 Pure Go. No external dependencies outside of the Go standard library. 10 11 12 ### INSTALLATION / UPDATING 13 14 go get -u github.com/disintegration/gift 15 16 17 18 ### DOCUMENTATION 19 20 http://godoc.org/github.com/disintegration/gift 21 22 23 24 ### QUICK START 25 26 ```go 27 // 1. Create a new GIFT filter list and add some filters: 28 g := gift.New( 29 gift.Resize(800, 0, gift.LanczosResampling), 30 gift.UnsharpMask(1.0, 1.0, 0.0), 31 ) 32 33 // 2. Create a new image of the corresponding size. 34 // dst is a new target image, src is the original image 35 dst := image.NewRGBA(g.Bounds(src.Bounds())) 36 37 // 3. Use Draw func to apply the filters to src and store the result in dst: 38 g.Draw(dst, src) 39 ``` 40 41 ### USAGE 42 43 To create a sequence of filters, the `New` function is used: 44 ```go 45 g := gift.New( 46 gift.Grayscale(), 47 gift.Contrast(10), 48 ) 49 ``` 50 Filters also can be added using the `Add` method: 51 ```go 52 g.Add(GaussianBlur(2)) 53 ``` 54 55 The `Bounds` method takes the bounds of the source image and returns appropriate bounds for the destination image to fit the result (for example, after using `Resize` or `Rotate` filters). 56 57 ```go 58 dst := image.NewRGBA(g.Bounds(src.Bounds())) 59 ``` 60 61 There are two methods available to apply these filters to an image: 62 63 - `Draw` applies all the added filters to the src image and outputs the result to the dst image starting from the top-left corner (Min point). 64 ```go 65 g.Draw(dst, src) 66 ``` 67 68 - `DrawAt` provides more control. It outputs the filtered src image to the dst image at the specified position using the specified image composition operator. This example is equivalent to the previous: 69 ```go 70 g.DrawAt(dst, src, dst.Bounds().Min, gift.CopyOperator) 71 ``` 72 73 Two image composition operators are supported by now: 74 - `CopyOperator` - Replaces pixels of the dst image with pixels of the filtered src image. This mode is used by the Draw method. 75 - `OverOperator` - Places the filtered src image on top of the dst image. This mode makes sence if the filtered src image has transparent areas. 76 77 Empty filter list can be used to create a copy of an image or to paste one image to another. For example: 78 ```go 79 // Create a new image with dimensions of bgImage 80 dstImage := image.NewNRGBA(bgImage.Bounds()) 81 // Copy the bgImage to the dstImage. 82 gift.New().Draw(dstImage, bgImage) 83 // Draw the fgImage over the dstImage at the (100, 100) position. 84 gift.New().DrawAt(dstImage, fgImage, image.Pt(100, 100), gift.OverOperator) 85 ``` 86 87 88 ### SUPPORTED FILTERS 89 90 + Transformations 91 92 - Crop(rect image.Rectangle) 93 - CropToSize(width, height int, anchor Anchor) 94 - FlipHorizontal() 95 - FlipVertical() 96 - Resize(width, height int, resampling Resampling) 97 - ResizeToFill(width, height int, resampling Resampling, anchor Anchor) 98 - ResizeToFit(width, height int, resampling Resampling) 99 - Rotate(angle float32, backgroundColor color.Color, interpolation Interpolation) 100 - Rotate180() 101 - Rotate270() 102 - Rotate90() 103 - Transpose() 104 - Transverse() 105 106 + Adjustments & effects 107 108 - Brightness(percentage float32) 109 - ColorBalance(percentageRed, percentageGreen, percentageBlue float32) 110 - ColorFunc(fn func(r0, g0, b0, a0 float32) (r, g, b, a float32)) 111 - Colorize(hue, saturation, percentage float32) 112 - ColorspaceLinearToSRGB() 113 - ColorspaceSRGBToLinear() 114 - Contrast(percentage float32) 115 - Convolution(kernel []float32, normalize, alpha, abs bool, delta float32) 116 - Gamma(gamma float32) 117 - GaussianBlur(sigma float32) 118 - Grayscale() 119 - Hue(shift float32) 120 - Invert() 121 - Maximum(ksize int, disk bool) 122 - Mean(ksize int, disk bool) 123 - Median(ksize int, disk bool) 124 - Minimum(ksize int, disk bool) 125 - Pixelate(size int) 126 - Saturation(percentage float32) 127 - Sepia(percentage float32) 128 - Sigmoid(midpoint, factor float32) 129 - Sobel() 130 - UnsharpMask(sigma, amount, thresold float32) 131 132 133 ### FILTER EXAMPLES 134 135 ##### Resize using lanczos resampling 136 ```go 137 gift.Resize(200, 0, gift.LanczosResampling) 138 ``` 139 Original image | Filtered image 140 --- | --- 141  |  142 143 ##### Resize using linear resampling 144 ```go 145 gift.Resize(200, 0, gift.LinearResampling) 146 ``` 147 Original image | Filtered image 148 --- | --- 149  |  150 151 ##### Resize to fit 160x160px bounding box 152 ```go 153 gift.ResizeToFit(160, 160, gift.LanczosResampling) 154 ``` 155 Original image | Filtered image 156 --- | --- 157  |  158 159 ##### Resize to fill 160x160px rectangle, anchor: center 160 ```go 161 gift.ResizeToFill(160, 160, gift.LanczosResampling, gift.CenterAnchor) 162 ``` 163 Original image | Filtered image 164 --- | --- 165  |  166 167 ##### Crop 90, 90 - 250, 250 168 ```go 169 gift.Crop(image.Rect(90, 90, 250, 250)) 170 ``` 171 Original image | Filtered image 172 --- | --- 173  |  174 175 ##### Crop to size 160x160px, anchor: center 176 ```go 177 gift.CropToSize(160, 160, gift.CenterAnchor) 178 ``` 179 Original image | Filtered image 180 --- | --- 181  |  182 183 ##### Rotate 90 degrees 184 ```go 185 gift.Rotate90() 186 ``` 187 Original image | Filtered image 188 --- | --- 189  |  190 191 ##### Rotate 180 degrees 192 ```go 193 gift.Rotate180() 194 ``` 195 Original image | Filtered image 196 --- | --- 197  |  198 199 ##### Rotate 270 degrees 200 ```go 201 gift.Rotate270() 202 ``` 203 Original image | Filtered image 204 --- | --- 205  |  206 207 ##### Rotate 30 degrees, white background, cubic interpolation 208 ```go 209 gift.Rotate(30, color.White, gift.CubicInterpolation) 210 ``` 211 Original image | Filtered image 212 --- | --- 213  |  214 215 ##### Flip horizontal 216 ```go 217 gift.FlipHorizontal() 218 ``` 219 Original image | Filtered image 220 --- | --- 221  |  222 223 ##### Flip vertical 224 ```go 225 gift.FlipVertical() 226 ``` 227 Original image | Filtered image 228 --- | --- 229  |  230 231 ##### Transpose 232 ```go 233 gift.Transpose() 234 ``` 235 Original image | Filtered image 236 --- | --- 237  |  238 239 ##### Transverse 240 ```go 241 gift.Transverse() 242 ``` 243 Original image | Filtered image 244 --- | --- 245  |  246 247 ##### Contrast +30% 248 ```go 249 gift.Contrast(30) 250 ``` 251 Original image | Filtered image 252 --- | --- 253  |  254 255 ##### Contrast -30% 256 ```go 257 gift.Contrast(-30) 258 ``` 259 Original image | Filtered image 260 --- | --- 261  |  262 263 ##### Brightness +30% 264 ```go 265 gift.Brightness(30) 266 ``` 267 Original image | Filtered image 268 --- | --- 269  |  270 271 ##### Brightness -30% 272 ```go 273 gift.Brightness(-30) 274 ``` 275 Original image | Filtered image 276 --- | --- 277  |  278 279 ##### Saturation +50% 280 ```go 281 gift.Saturation(50) 282 ``` 283 Original image | Filtered image 284 --- | --- 285  |  286 287 ##### Saturation -50% 288 ```go 289 gift.Saturation(-50) 290 ``` 291 Original image | Filtered image 292 --- | --- 293  |  294 295 ##### Hue +45 296 ```go 297 gift.Hue(45) 298 ``` 299 Original image | Filtered image 300 --- | --- 301  |  302 303 ##### Hue -45 304 ```go 305 gift.Hue(-45) 306 ``` 307 Original image | Filtered image 308 --- | --- 309  |  310 311 ##### Sigmoid 0.5, 5.0 312 ```go 313 gift.Sigmoid(0.5, 5.0) 314 ``` 315 Original image | Filtered image 316 --- | --- 317  |  318 319 ##### Gamma correction = 0.5 320 ```go 321 gift.Gamma(0.5) 322 ``` 323 Original image | Filtered image 324 --- | --- 325  |  326 327 ##### Gamma correction = 1.5 328 ```go 329 gift.Gamma(1.5) 330 ``` 331 Original image | Filtered image 332 --- | --- 333  |  334 335 ##### Gaussian blur, sigma=1.0 336 ```go 337 gift.GaussianBlur(1.0) 338 ``` 339 Original image | Filtered image 340 --- | --- 341  |  342 343 ##### Unsharp mask, sigma=1.0, amount=1.5, thresold=0.0 344 ```go 345 gift.UnsharpMask(1.0, 1.5, 0.0) 346 ``` 347 Original image | Filtered image 348 --- | --- 349  |  350 351 ##### Grayscale 352 ```go 353 gift.Grayscale() 354 ``` 355 Original image | Filtered image 356 --- | --- 357  |  358 359 ##### Sepia, 100% 360 ```go 361 gift.Sepia(100) 362 ``` 363 Original image | Filtered image 364 --- | --- 365  |  366 367 ##### Invert 368 ```go 369 gift.Invert() 370 ``` 371 Original image | Filtered image 372 --- | --- 373  |  374 375 ##### Colorize, blue, saturation=50% 376 ```go 377 gift.Colorize(240, 50, 100) 378 ``` 379 Original image | Filtered image 380 --- | --- 381  |  382 383 ##### Color balance, +20% red, -20% green 384 ```go 385 gift.ColorBalance(20, -20, 0) 386 ``` 387 Original image | Filtered image 388 --- | --- 389  |  390 391 ##### Color function 392 ```go 393 gift.ColorFunc( 394 func(r0, g0, b0, a0 float32) (r, g, b, a float32) { 395 r = 1 - r0 // invert the red channel 396 g = g0 + 0.1 // shift the green channel by 0.1 397 b = 0 // set the blue channel to 0 398 a = a0 // preserve the alpha channel 399 return 400 }, 401 ) 402 ``` 403 Original image | Filtered image 404 --- | --- 405  |  406 407 ##### Local mean, disc shape, size=5 408 ```go 409 gift.Mean(5, true) 410 ``` 411 Original image | Filtered image 412 --- | --- 413  |  414 415 ##### Local median, disc shape, size=5 416 ```go 417 gift.Median(5, true) 418 ``` 419 Original image | Filtered image 420 --- | --- 421  |  422 423 ##### Local minimum, disc shape, size=5 424 ```go 425 gift.Minimum(5, true) 426 ``` 427 Original image | Filtered image 428 --- | --- 429  |  430 431 ##### Local maximum, disc shape, size=5 432 ```go 433 gift.Maximum(5, true) 434 ``` 435 Original image | Filtered image 436 --- | --- 437  |  438 439 ##### Pixelate, size=5 440 ```go 441 gift.Pixelate(5) 442 ``` 443 Original image | Filtered image 444 --- | --- 445  |  446 447 ##### Convolution matrix - Emboss 448 ```go 449 gift.Convolution( 450 []float32{ 451 -1, -1, 0, 452 -1, 1, 1, 453 0, 1, 1, 454 }, 455 false, false, false, 0.0, 456 ) 457 ``` 458 Original image | Filtered image 459 --- | --- 460  |  461 462 ##### Convolution matrix - Edge detection 463 ```go 464 gift.Convolution( 465 []float32{ 466 -1, -1, -1, 467 -1, 8, -1, 468 -1, -1, -1, 469 }, 470 false, false, false, 0.0, 471 ) 472 ``` 473 Original image | Filtered image 474 --- | --- 475  |  476 477 ##### Sobel operator 478 ```go 479 gift.Sobel() 480 ``` 481 Original image | Filtered image 482 --- | --- 483  |  484