Debian Codegraph @

README.md

raw symbols SCIP
1 # GO IMAGE FILTERING TOOLKIT (GIFT)
2 [![GoDoc](https://godoc.org/github.com/gohugoio/gift?status.svg)](https://godoc.org/github.com/gohugoio/gift)
3 [![Tests on Linux, MacOS and Windows](https://github.com/gohugoio/gift/workflows/Test/badge.svg)](https://github.com/gohugoio/gift/actions?query=workflow%3ATest)
4 [![Go Report Card](https://goreportcard.com/badge/github.com/gohugoio/gift)](https://goreportcard.com/report/github.com/gohugoio/gift)
5 [![Go Report Card](https://goreportcard.com/badge/github.com/gohugoio/gift)](https://goreportcard.com/report/github.com/gohugoio/gift)
6
7
8 *Package gift provides a set of useful image processing filters.*
9
10 Pure Go. No external dependencies outside of the Go standard library.
11
12
13 ### INSTALLATION / UPDATING
14
15 go get -u github.com/gohugoio/gift
16
17
18 ### DOCUMENTATION
19
20 http://godoc.org/github.com/gohugoio/gift
21
22
23 ### QUICK START
24
25 ```go
26 // 1. Create a new filter list and add some filters.
27 g := gift.New(
28 gift.Resize(800, 0, gift.LanczosResampling),
29 gift.UnsharpMask(1, 1, 0),
30 )
31
32 // 2. Create a new image of the corresponding size.
33 // dst is a new target image, src is the original image.
34 dst := image.NewRGBA(g.Bounds(src.Bounds()))
35
36 // 3. Use the Draw func to apply the filters to src and store the result in dst.
37 g.Draw(dst, src)
38 ```
39
40 ### USAGE
41
42 To create a sequence of filters, the `New` function is used:
43 ```go
44 g := gift.New(
45 gift.Grayscale(),
46 gift.Contrast(10),
47 )
48 ```
49 Filters also can be added using the `Add` method:
50 ```go
51 g.Add(GaussianBlur(2))
52 ```
53
54 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).
55
56 ```go
57 dst := image.NewRGBA(g.Bounds(src.Bounds()))
58 ```
59
60 There are two methods available to apply these filters to an image:
61
62 - `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).
63 ```go
64 g.Draw(dst, src)
65 ```
66
67 - `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:
68 ```go
69 g.DrawAt(dst, src, dst.Bounds().Min, gift.CopyOperator)
70 ```
71
72 Two image composition operators are supported by now:
73 - `CopyOperator` - Replaces pixels of the dst image with pixels of the filtered src image. This mode is used by the Draw method.
74 - `OverOperator` - Places the filtered src image on top of the dst image. This mode makes sence if the filtered src image has transparent areas.
75
76 Empty filter list can be used to create a copy of an image or to paste one image to another. For example:
77 ```go
78 // Create a new image with dimensions of the bgImage.
79 dstImage := image.NewRGBA(bgImage.Bounds())
80 // Copy the bgImage to the dstImage.
81 gift.New().Draw(dstImage, bgImage)
82 // Draw the fgImage over the dstImage at the (100, 100) position.
83 gift.New().DrawAt(dstImage, fgImage, image.Pt(100, 100), gift.OverOperator)
84 ```
85
86
87 ### SUPPORTED FILTERS
88
89 + Transformations
90
91 - Crop(rect image.Rectangle)
92 - CropToSize(width, height int, anchor Anchor)
93 - FlipHorizontal()
94 - FlipVertical()
95 - Resize(width, height int, resampling Resampling)
96 - ResizeToFill(width, height int, resampling Resampling, anchor Anchor)
97 - ResizeToFit(width, height int, resampling Resampling)
98 - Rotate(angle float32, backgroundColor color.Color, interpolation Interpolation)
99 - Rotate180()
100 - Rotate270()
101 - Rotate90()
102 - Transpose()
103 - Transverse()
104
105 + Adjustments & effects
106
107 - Brightness(percentage float32)
108 - ColorBalance(percentageRed, percentageGreen, percentageBlue float32)
109 - ColorFunc(fn func(r0, g0, b0, a0 float32) (r, g, b, a float32))
110 - Colorize(hue, saturation, percentage float32)
111 - ColorspaceLinearToSRGB()
112 - ColorspaceSRGBToLinear()
113 - Contrast(percentage float32)
114 - Convolution(kernel []float32, normalize, alpha, abs bool, delta float32)
115 - Gamma(gamma float32)
116 - GaussianBlur(sigma float32)
117 - Grayscale()
118 - Hue(shift float32)
119 - Invert()
120 - Maximum(ksize int, disk bool)
121 - Mean(ksize int, disk bool)
122 - Median(ksize int, disk bool)
123 - Minimum(ksize int, disk bool)
124 - Pixelate(size int)
125 - Saturation(percentage float32)
126 - Sepia(percentage float32)
127 - Sigmoid(midpoint, factor float32)
128 - Sobel()
129 - Threshold(percentage float32)
130 - UnsharpMask(sigma, amount, threshold float32)
131
132
133 ### FILTER EXAMPLES
134
135 The original image:
136
137 ![](testdata/src.png)
138
139 Resulting images after applying some of the filters:
140
141 name / result | name / result | name / result | name / result
142 --------------------------------------------|--------------------------------------------|--------------------------------------------|--------------------------------------------
143 resize | crop_to_size | rotate_180 | rotate_30
144 ![](testdata/dst_resize.png) | ![](testdata/dst_crop_to_size.png) | ![](testdata/dst_rotate_180.png) | ![](testdata/dst_rotate_30.png)
145 brightness_increase | brightness_decrease | contrast_increase | contrast_decrease
146 ![](testdata/dst_brightness_increase.png) | ![](testdata/dst_brightness_decrease.png) | ![](testdata/dst_contrast_increase.png) | ![](testdata/dst_contrast_decrease.png)
147 saturation_increase | saturation_decrease | gamma_1.5 | gamma_0.5
148 ![](testdata/dst_saturation_increase.png) | ![](testdata/dst_saturation_decrease.png) | ![](testdata/dst_gamma_1.5.png) | ![](testdata/dst_gamma_0.5.png)
149 gaussian_blur | unsharp_mask | sigmoid | pixelate
150 ![](testdata/dst_gaussian_blur.png) | ![](testdata/dst_unsharp_mask.png) | ![](testdata/dst_sigmoid.png) | ![](testdata/dst_pixelate.png)
151 colorize | grayscale | sepia | invert
152 ![](testdata/dst_colorize.png) | ![](testdata/dst_grayscale.png) | ![](testdata/dst_sepia.png) | ![](testdata/dst_invert.png)
153 mean | median | minimum | maximum
154 ![](testdata/dst_mean.png) | ![](testdata/dst_median.png) | ![](testdata/dst_minimum.png) | ![](testdata/dst_maximum.png)
155 hue_rotate | color_balance | color_func | convolution_emboss
156 ![](testdata/dst_hue_rotate.png) | ![](testdata/dst_color_balance.png) | ![](testdata/dst_color_func.png) | ![](testdata/dst_convolution_emboss.png)
157
158 Here's the code that produces the above images:
159
160 ```go
161 package main
162
163 import (
164 "image"
165 "image/color"
166 "image/png"
167 "log"
168 "os"
169
170 "github.com/gohugoio/gift"
171 )
172
173 func main() {
174 src := loadImage("testdata/src.png")
175
176 filters := map[string]gift.Filter{
177 "resize": gift.Resize(100, 0, gift.LanczosResampling),
178 "crop_to_size": gift.CropToSize(100, 100, gift.LeftAnchor),
179 "rotate_180": gift.Rotate180(),
180 "rotate_30": gift.Rotate(30, color.Transparent, gift.CubicInterpolation),
181 "brightness_increase": gift.Brightness(30),
182 "brightness_decrease": gift.Brightness(-30),
183 "contrast_increase": gift.Contrast(30),
184 "contrast_decrease": gift.Contrast(-30),
185 "saturation_increase": gift.Saturation(50),
186 "saturation_decrease": gift.Saturation(-50),
187 "gamma_1.5": gift.Gamma(1.5),
188 "gamma_0.5": gift.Gamma(0.5),
189 "gaussian_blur": gift.GaussianBlur(1),
190 "unsharp_mask": gift.UnsharpMask(1, 1, 0),
191 "sigmoid": gift.Sigmoid(0.5, 7),
192 "pixelate": gift.Pixelate(5),
193 "colorize": gift.Colorize(240, 50, 100),
194 "grayscale": gift.Grayscale(),
195 "sepia": gift.Sepia(100),
196 "invert": gift.Invert(),
197 "mean": gift.Mean(5, true),
198 "median": gift.Median(5, true),
199 "minimum": gift.Minimum(5, true),
200 "maximum": gift.Maximum(5, true),
201 "hue_rotate": gift.Hue(45),
202 "color_balance": gift.ColorBalance(10, -10, -10),
203 "color_func": gift.ColorFunc(
204 func(r0, g0, b0, a0 float32) (r, g, b, a float32) {
205 r = 1 - r0 // invert the red channel
206 g = g0 + 0.1 // shift the green channel by 0.1
207 b = 0 // set the blue channel to 0
208 a = a0 // preserve the alpha channel
209 return r, g, b, a
210 },
211 ),
212 "convolution_emboss": gift.Convolution(
213 []float32{
214 -1, -1, 0,
215 -1, 1, 1,
216 0, 1, 1,
217 },
218 false, false, false, 0.0,
219 ),
220 }
221
222 for name, filter := range filters {
223 g := gift.New(filter)
224 dst := image.NewNRGBA(g.Bounds(src.Bounds()))
225 g.Draw(dst, src)
226 saveImage("testdata/dst_"+name+".png", dst)
227 }
228 }
229
230 func loadImage(filename string) image.Image {
231 f, err := os.Open(filename)
232 if err != nil {
233 log.Fatalf("os.Open failed: %v", err)
234 }
235 defer f.Close()
236 img, _, err := image.Decode(f)
237 if err != nil {
238 log.Fatalf("image.Decode failed: %v", err)
239 }
240 return img
241 }
242
243 func saveImage(filename string, img image.Image) {
244 f, err := os.Create(filename)
245 if err != nil {
246 log.Fatalf("os.Create failed: %v", err)
247 }
248 defer f.Close()
249 err = png.Encode(f, img)
250 if err != nil {
251 log.Fatalf("png.Encode failed: %v", err)
252 }
253 }
254 ```