-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage.go
More file actions
82 lines (74 loc) · 2.14 KB
/
image.go
File metadata and controls
82 lines (74 loc) · 2.14 KB
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
package gg
import (
"image"
"os"
"github.com/fumiama/imgsz"
"golang.org/x/image/draw"
)
// ImageToRGBA converts an image.Image to *image.RGBA.
//
// ImageToRGBA 将 image.Image 转换为 *image.RGBA。
func ImageToRGBA(src image.Image) *image.RGBA {
bounds := src.Bounds()
dst := image.NewRGBA(bounds)
draw.Draw(dst, bounds, src, bounds.Min, draw.Src)
return dst
}
// ImageToRGBA64 converts an image.Image to *image.RGBA64.
//
// ImageToRGBA64 将 image.Image 转换为 *image.RGBA64。
func ImageToRGBA64(src image.Image) *image.RGBA64 {
bounds := src.Bounds()
dst := image.NewRGBA64(bounds)
draw.Draw(dst, bounds, src, bounds.Min, draw.Src)
return dst
}
// ImageToNRGBA converts an image.Image to *image.NRGBA.
//
// ImageToNRGBA 将 image.Image 转换为 *image.NRGBA。
func ImageToNRGBA(src image.Image) *image.NRGBA {
bounds := src.Bounds()
dst := image.NewNRGBA(bounds)
draw.Draw(dst, bounds, src, bounds.Min, draw.Src)
return dst
}
// ImageToNRGBA64 converts an image.Image to *image.NRGBA64.
//
// ImageToNRGBA64 将 image.Image 转换为 *image.NRGBA64。
func ImageToNRGBA64(src image.Image) *image.NRGBA64 {
bounds := src.Bounds()
dst := image.NewNRGBA64(bounds)
draw.Draw(dst, bounds, src, bounds.Min, draw.Src)
return dst
}
// GetImageWxH returns the width and height of the image at the given path.
//
// GetImageWxH 返回指定路径图片的宽度和高度。
func GetImageWxH(path string) (int, int, error) {
f, err := os.Open(path)
if err != nil {
return 0, 0, err
}
defer f.Close()
sz, _, err := imgsz.DecodeSize(f)
return sz.Width, sz.Height, err
}
// ImageBoundsBelow returns resized image that newW < w and newH < h, while keeping the W/H ratio.
//
// ImageBoundsBelow 返回在保持宽高比的条件下,小于 w x h 的新 bound。
func ImageBoundsBelow(b image.Rectangle, w, h int) image.Rectangle {
width := b.Dx()
height := b.Dy()
dstw, dsth := width, height
if dstw > w {
dstw = w
ratio := float64(dstw) / float64(width)
dsth *= int(float64(height) * ratio)
}
if dsth > h {
dsth = h
ratio := float64(dsth) / float64(height)
dstw = int(float64(width) * ratio)
}
return image.Rect(0, 0, dstw, dsth)
}