-
Notifications
You must be signed in to change notification settings - Fork 242
/
manipulation.go
230 lines (195 loc) · 6.33 KB
/
manipulation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package images
import (
"bytes"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"math"
"os"
"github.com/nfnt/resize"
"github.com/oliamb/cutter"
"go.uber.org/zap"
xdraw "golang.org/x/image/draw"
"github.com/ethereum/go-ethereum/log"
)
type Circle struct {
X, Y, R int
}
func (c *Circle) ColorModel() color.Model {
return color.AlphaModel
}
func (c *Circle) Bounds() image.Rectangle {
return image.Rect(c.X-c.R, c.Y-c.R, c.X+c.R, c.Y+c.R)
}
func (c *Circle) At(x, y int) color.Color {
xx, yy, rr := float64(x-c.X)+0.5, float64(y-c.Y)+0.5, float64(c.R)
if xx*xx+yy*yy < rr*rr {
return color.Alpha{255}
}
return color.Alpha{0}
}
func Resize(size ResizeDimension, img image.Image) image.Image {
var width, height uint
switch {
case img.Bounds().Max.X == img.Bounds().Max.Y:
width, height = uint(size), uint(size)
case img.Bounds().Max.X > img.Bounds().Max.Y:
width, height = 0, uint(size)
default:
width, height = uint(size), 0
}
log.Info("resizing", "size", size, "width", width, "height", height)
return resize.Resize(width, height, img, resize.Bilinear)
}
func ResizeTo(percent int, img image.Image) image.Image {
width := uint(img.Bounds().Max.X * percent / 100)
height := uint(img.Bounds().Max.Y * percent / 100)
return resize.Resize(width, height, img, resize.Bilinear)
}
func ShrinkOnly(size ResizeDimension, img image.Image) image.Image {
finalSize := int(math.Min(float64(size), math.Min(float64(img.Bounds().Dx()), float64(img.Bounds().Dy()))))
return Resize(ResizeDimension(finalSize), img)
}
func Crop(img image.Image, rect image.Rectangle) (image.Image, error) {
if img.Bounds().Max.X < rect.Max.X || img.Bounds().Max.Y < rect.Max.Y {
return nil, fmt.Errorf(
"crop dimensions out of bounds of image, image width '%dpx' & height '%dpx'; crop bottom right coordinate at X '%dpx' Y '%dpx'",
img.Bounds().Max.X, img.Bounds().Max.Y,
rect.Max.X, rect.Max.Y,
)
}
return cutter.Crop(img, cutter.Config{
Width: rect.Dx(),
Height: rect.Dy(),
Anchor: rect.Min,
})
}
// CropCenter takes an image, usually downloaded from a URL
// If the image is square, the full image is returned
// If the image is rectangular, the largest central square is returned
// calculations at _docs/image-center-crop-calculations.png
func CropCenter(img image.Image) (image.Image, error) {
var cropRect image.Rectangle
maxBounds := img.Bounds().Max
if maxBounds.X == maxBounds.Y {
return img, nil
}
if maxBounds.X > maxBounds.Y {
// the final output should be YxY
cropRect = image.Rectangle{
Min: image.Point{X: maxBounds.X/2 - maxBounds.Y/2, Y: 0},
Max: image.Point{X: maxBounds.X/2 + maxBounds.Y/2, Y: maxBounds.Y},
}
} else {
// the final output should be XxX
cropRect = image.Rectangle{
Min: image.Point{X: 0, Y: maxBounds.Y/2 - maxBounds.X/2},
Max: image.Point{X: maxBounds.X, Y: maxBounds.Y/2 + maxBounds.X/2},
}
}
return Crop(img, cropRect)
}
func ImageToBytes(imagePath string) ([]byte, error) {
// Open the image file
file, err := os.Open(imagePath)
if err != nil {
return nil, err
}
defer file.Close()
// Decode the image
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
// Create a new buffer to hold the image data
var imgBuffer bytes.Buffer
// Encode the image to the desired format and save it in the buffer
err = png.Encode(&imgBuffer, img)
if err != nil {
return nil, err
}
// Return the image data as a byte slice
return imgBuffer.Bytes(), nil
}
func AddPadding(img image.Image, padding int) *image.RGBA {
bounds := img.Bounds()
newBounds := image.Rect(bounds.Min.X-padding, bounds.Min.Y-padding, bounds.Max.X+padding, bounds.Max.Y+padding)
paddedImg := image.NewRGBA(newBounds)
draw.Draw(paddedImg, newBounds, &image.Uniform{C: color.White}, image.ZP, draw.Src)
return paddedImg
}
func EncodePNG(img *image.RGBA) ([]byte, error) {
resultImg := &bytes.Buffer{}
err := png.Encode(resultImg, img)
if err != nil {
return nil, err
}
return resultImg.Bytes(), nil
}
func CreateCircle(img image.Image) *image.RGBA {
bounds := img.Bounds()
circle := image.NewRGBA(bounds)
draw.DrawMask(circle, bounds, img, image.ZP, &Circle{
X: bounds.Dx() / 2,
Y: bounds.Dy() / 2,
R: bounds.Dx() / 2,
}, image.ZP, draw.Over)
return circle
}
func PlaceCircleInCenter(paddedImg, circle *image.RGBA) *image.RGBA {
bounds := circle.Bounds()
centerX := (paddedImg.Bounds().Min.X + paddedImg.Bounds().Max.X) / 2
centerY := (paddedImg.Bounds().Min.Y + paddedImg.Bounds().Max.Y) / 2
draw.Draw(paddedImg, bounds.Add(image.Pt(centerX-bounds.Dx()/2, centerY-bounds.Dy()/2)), circle, image.ZP, draw.Over)
return paddedImg
}
func ResizeImage(imgBytes []byte, width, height int) ([]byte, error) {
// Decode image bytes
img, _, err := image.Decode(bytes.NewReader(imgBytes))
if err != nil {
return nil, err
}
// Create a new image with the desired dimensions
newImg := image.NewNRGBA(image.Rect(0, 0, width, height))
xdraw.BiLinear.Scale(newImg, newImg.Bounds(), img, img.Bounds(), draw.Over, nil)
// Encode the new image to bytes
var newImgBytes bytes.Buffer
if err = png.Encode(&newImgBytes, newImg); err != nil {
return nil, err
}
return newImgBytes.Bytes(), nil
}
func SuperimposeLogoOnQRImage(imageBytes []byte, qrFilepath []byte) []byte {
// Read the two images from bytes
img1, _, err := image.Decode(bytes.NewReader(imageBytes))
if err != nil {
log.Error("error decoding logo Image", zap.Error(err))
return nil
}
img2, _, err := image.Decode(bytes.NewReader(qrFilepath))
if err != nil {
log.Error("error decoding QR Image", zap.Error(err))
return nil
}
// Create a new image with the dimensions of the first image
result := image.NewRGBA(img1.Bounds())
// Draw the first image on the new image
draw.Draw(result, img1.Bounds(), img1, image.ZP, draw.Src)
// Get the dimensions of the second image
img2Bounds := img2.Bounds()
// Calculate the x and y coordinates to center the second image
x := (img1.Bounds().Dx() - img2Bounds.Dx()) / 2
y := (img1.Bounds().Dy() - img2Bounds.Dy()) / 2
// Draw the second image on top of the first image at the calculated coordinates
draw.Draw(result, img2Bounds.Add(image.Pt(x, y)), img2, image.ZP, draw.Over)
// Encode the final image to a desired format
var b bytes.Buffer
err = png.Encode(&b, result)
if err != nil {
log.Error("error encoding final result Image to Buffer", zap.Error(err))
return nil
}
return b.Bytes()
}