• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

testdata/H03-May-2022-

.travis.ymlH A D24-Aug-2019188

LICENSEH A D24-Aug-20191.1 KiB

README.mdH A D24-Aug-20199.7 KiB

colors.goH A D24-Aug-201912.2 KiB

colors_test.goH A D24-Aug-201926.9 KiB

convolution.goH A D24-Aug-201913.6 KiB

convolution_test.goH A D24-Aug-201914.7 KiB

effects.goH A D24-Aug-20191.9 KiB

effects_test.goH A D24-Aug-20192.3 KiB

gift.goH A D24-Aug-20195.6 KiB

gift_test.goH A D24-Aug-201918.4 KiB

go.modH A D24-Aug-201938

pixels.goH A D24-Aug-201910.5 KiB

pixels_test.goH A D24-Aug-201915.2 KiB

rank.goH A D24-Aug-20194.9 KiB

rank_test.goH A D24-Aug-201911.9 KiB

resize.goH A D24-Aug-201910.8 KiB

resize_test.goH A D24-Aug-20199.1 KiB

transform.goH A D24-Aug-201912.6 KiB

transform_test.goH A D24-Aug-201914.3 KiB

utils.goH A D24-Aug-20193.8 KiB

utils_test.goH A D24-Aug-20196.1 KiB

README.md

1# GO IMAGE FILTERING TOOLKIT (GIFT)
2
3[![GoDoc](https://godoc.org/github.com/disintegration/gift?status.svg)](https://godoc.org/github.com/disintegration/gift)
4[![Build Status](https://travis-ci.org/disintegration/gift.svg?branch=master)](https://travis-ci.org/disintegration/gift)
5[![Coverage Status](https://coveralls.io/repos/github/disintegration/gift/badge.svg?branch=master)](https://coveralls.io/github/disintegration/gift?branch=master)
6[![Go Report Card](https://goreportcard.com/badge/github.com/disintegration/gift)](https://goreportcard.com/report/github.com/disintegration/gift)
7
8
9*Package gift provides a set of useful image processing filters.*
10
11Pure Go. No external dependencies outside of the Go standard library.
12
13
14### INSTALLATION / UPDATING
15
16    go get -u github.com/disintegration/gift
17
18
19### DOCUMENTATION
20
21http://godoc.org/github.com/disintegration/gift
22
23
24### QUICK START
25
26```go
27// 1. Create a new filter list and add some filters.
28g := gift.New(
29	gift.Resize(800, 0, gift.LanczosResampling),
30	gift.UnsharpMask(1, 1, 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.
35dst := image.NewRGBA(g.Bounds(src.Bounds()))
36
37// 3. Use the Draw func to apply the filters to src and store the result in dst.
38g.Draw(dst, src)
39```
40
41### USAGE
42
43To create a sequence of filters, the `New` function is used:
44```go
45g := gift.New(
46	gift.Grayscale(),
47	gift.Contrast(10),
48)
49```
50Filters also can be added using the `Add` method:
51```go
52g.Add(GaussianBlur(2))
53```
54
55The `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
58dst := image.NewRGBA(g.Bounds(src.Bounds()))
59```
60
61There 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
65g.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
70g.DrawAt(dst, src, dst.Bounds().Min, gift.CopyOperator)
71```
72
73Two 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
77Empty 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 the bgImage.
80dstImage := image.NewRGBA(bgImage.Bounds())
81// Copy the bgImage to the dstImage.
82gift.New().Draw(dstImage, bgImage)
83// Draw the fgImage over the dstImage at the (100, 100) position.
84gift.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    - Threshold(percentage float32)
131    - UnsharpMask(sigma, amount, threshold float32)
132
133
134### FILTER EXAMPLES
135
136The original image:
137
138![](testdata/src.png)
139
140Resulting images after applying some of the filters:
141
142 name / result                              | name / result                              | name / result                              | name / result
143--------------------------------------------|--------------------------------------------|--------------------------------------------|--------------------------------------------
144resize                                      | crop_to_size                               | rotate_180                                 | rotate_30
145![](testdata/dst_resize.png)                | ![](testdata/dst_crop_to_size.png)         | ![](testdata/dst_rotate_180.png)           | ![](testdata/dst_rotate_30.png)
146brightness_increase                         | brightness_decrease                        | contrast_increase                          | contrast_decrease
147![](testdata/dst_brightness_increase.png)   | ![](testdata/dst_brightness_decrease.png)  | ![](testdata/dst_contrast_increase.png)    | ![](testdata/dst_contrast_decrease.png)
148saturation_increase                         | saturation_decrease                        | gamma_1.5                                  | gamma_0.5
149![](testdata/dst_saturation_increase.png)   | ![](testdata/dst_saturation_decrease.png)  | ![](testdata/dst_gamma_1.5.png)            | ![](testdata/dst_gamma_0.5.png)
150gaussian_blur                               | unsharp_mask                               | sigmoid                                    | pixelate
151![](testdata/dst_gaussian_blur.png)         | ![](testdata/dst_unsharp_mask.png)         | ![](testdata/dst_sigmoid.png)              | ![](testdata/dst_pixelate.png)
152colorize                                    | grayscale                                  | sepia                                      | invert
153![](testdata/dst_colorize.png)              | ![](testdata/dst_grayscale.png)            | ![](testdata/dst_sepia.png)                | ![](testdata/dst_invert.png)
154mean                                        | median                                     | minimum                                    | maximum
155![](testdata/dst_mean.png)                  | ![](testdata/dst_median.png)               | ![](testdata/dst_minimum.png)              | ![](testdata/dst_maximum.png)
156hue_rotate                                  | color_balance                              | color_func                                 | convolution_emboss
157![](testdata/dst_hue_rotate.png)            | ![](testdata/dst_color_balance.png)        | ![](testdata/dst_color_func.png)           | ![](testdata/dst_convolution_emboss.png)
158
159Here's the code that produces the above images:
160
161```go
162package main
163
164import (
165	"image"
166	"image/color"
167	"image/png"
168	"log"
169	"os"
170
171	"github.com/disintegration/gift"
172)
173
174func main() {
175	src := loadImage("testdata/src.png")
176
177	filters := map[string]gift.Filter{
178		"resize":               gift.Resize(100, 0, gift.LanczosResampling),
179		"crop_to_size":         gift.CropToSize(100, 100, gift.LeftAnchor),
180		"rotate_180":           gift.Rotate180(),
181		"rotate_30":            gift.Rotate(30, color.Transparent, gift.CubicInterpolation),
182		"brightness_increase":  gift.Brightness(30),
183		"brightness_decrease":  gift.Brightness(-30),
184		"contrast_increase":    gift.Contrast(30),
185		"contrast_decrease":    gift.Contrast(-30),
186		"saturation_increase":  gift.Saturation(50),
187		"saturation_decrease":  gift.Saturation(-50),
188		"gamma_1.5":            gift.Gamma(1.5),
189		"gamma_0.5":            gift.Gamma(0.5),
190		"gaussian_blur":        gift.GaussianBlur(1),
191		"unsharp_mask":         gift.UnsharpMask(1, 1, 0),
192		"sigmoid":              gift.Sigmoid(0.5, 7),
193		"pixelate":             gift.Pixelate(5),
194		"colorize":             gift.Colorize(240, 50, 100),
195		"grayscale":            gift.Grayscale(),
196		"sepia":                gift.Sepia(100),
197		"invert":               gift.Invert(),
198		"mean":                 gift.Mean(5, true),
199		"median":               gift.Median(5, true),
200		"minimum":              gift.Minimum(5, true),
201		"maximum":              gift.Maximum(5, true),
202		"hue_rotate":           gift.Hue(45),
203		"color_balance":        gift.ColorBalance(10, -10, -10),
204		"color_func": gift.ColorFunc(
205			func(r0, g0, b0, a0 float32) (r, g, b, a float32) {
206				r = 1 - r0   // invert the red channel
207				g = g0 + 0.1 // shift the green channel by 0.1
208				b = 0        // set the blue channel to 0
209				a = a0       // preserve the alpha channel
210				return r, g, b, a
211			},
212		),
213		"convolution_emboss": gift.Convolution(
214			[]float32{
215				-1, -1, 0,
216				-1, 1, 1,
217				0, 1, 1,
218			},
219			false, false, false, 0.0,
220		),
221	}
222
223	for name, filter := range filters {
224		g := gift.New(filter)
225		dst := image.NewNRGBA(g.Bounds(src.Bounds()))
226		g.Draw(dst, src)
227		saveImage("testdata/dst_"+name+".png", dst)
228	}
229}
230
231func loadImage(filename string) image.Image {
232	f, err := os.Open(filename)
233	if err != nil {
234		log.Fatalf("os.Open failed: %v", err)
235	}
236	defer f.Close()
237	img, _, err := image.Decode(f)
238	if err != nil {
239		log.Fatalf("image.Decode failed: %v", err)
240	}
241	return img
242}
243
244func saveImage(filename string, img image.Image) {
245	f, err := os.Create(filename)
246	if err != nil {
247		log.Fatalf("os.Create failed: %v", err)
248	}
249	defer f.Close()
250	err = png.Encode(f, img)
251	if err != nil {
252		log.Fatalf("png.Encode failed: %v", err)
253	}
254}
255```
256