-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-processing.go
53 lines (43 loc) · 1.65 KB
/
image-processing.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
package main
import (
"image"
"image/color"
"image/draw"
"github.com/nfnt/resize"
)
func resizeImage(img image.Image, width, height int) (image.Image, error) {
outputRatio := float64(width) / float64(height)
inputWidth := img.Bounds().Max.X - img.Bounds().Min.X
inputHeight := img.Bounds().Max.Y - img.Bounds().Min.Y
inputRatio := float64(inputWidth) / float64(inputHeight)
var resizeWidth, resizeHeight uint
if outputRatio > inputRatio {
resizeWidth = uint(float64(height) * inputRatio)
resizeHeight = uint(height)
} else {
resizeWidth = uint(width)
resizeHeight = uint(float64(width) / inputRatio)
}
resizedImg := resize.Resize(resizeWidth, resizeHeight, img, resize.NearestNeighbor)
return resizedImg, nil
}
func createImage(width, height int, bgColor color.RGBA) *image.RGBA {
outputImg := image.NewRGBA(image.Rect(0, 0, width, height))
draw.Draw(outputImg, outputImg.Bounds(), &image.Uniform{C: bgColor}, image.Point{}, draw.Src)
return outputImg
}
func copyImage(srcImg image.Image, destImg draw.Image) {
resizeWidth := srcImg.Bounds().Max.X - srcImg.Bounds().Min.X
resizeHeight := srcImg.Bounds().Max.Y - srcImg.Bounds().Min.Y
offsetX := (destImg.Bounds().Max.X - resizeWidth) / 2
offsetY := (destImg.Bounds().Max.Y - resizeHeight) / 2
// Convert the source image to a draw.Image object
srcDrawImg := drawImageFromImage(srcImg)
draw.Draw(destImg, image.Rect(offsetX, offsetY, offsetX+resizeWidth, offsetY+resizeHeight), srcDrawImg, srcDrawImg.Bounds().Min, draw.Src)
}
func drawImageFromImage(img image.Image) draw.Image {
bounds := img.Bounds()
dest := image.NewRGBA(bounds)
draw.Draw(dest, bounds, img, bounds.Min, draw.Src)
return dest
}