-
Notifications
You must be signed in to change notification settings - Fork 4
/
draw.go
70 lines (61 loc) · 1.33 KB
/
draw.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
package main
import (
"image"
"image/color"
"image/draw"
"image/jpeg"
"os"
)
func HLine(img draw.Image, x1, y, x2 int, col color.Color) {
for ; x1 <= x2; x1++ {
img.Set(x1, y, col)
img.Set(x1, y+1, col)
img.Set(x1, y+2, col)
}
}
func VLine(img draw.Image, x, y1, y2 int, col color.Color) {
for ; y1 <= y2; y1++ {
img.Set(x, y1, col)
img.Set(x+1, y1, col)
img.Set(x+2, y1, col)
}
}
func Rect(img draw.Image, x1, y1, w, h int, col color.Color) {
HLine(img, x1, y1, x1+w, col)
HLine(img, x1, y1+h, x1+w, col)
VLine(img, x1, y1, y1+h, col)
VLine(img, x1+w, y1, y1+h, col)
}
func drawBoxes(fname string, x float32, y float32, w float32, h float32) error {
f, err := os.Open(fname)
if err != nil {
return err
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
return err
}
b := img.Bounds()
dst := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
draw.Draw(dst, b, img, b.Min, draw.Src)
col := color.RGBA{255, 0, 0, 128}
// HLine(dst, 10, 200, 200, col)
// for i := 0; i < 4; i++ {
Rect(dst, int(float32(b.Dx())*x), int(float32(b.Dy())*y), int(float32(b.Dx())*w), int(float32(b.Dy())*h), col)
// }
f.Close()
f, err = os.Create(fname)
if err != nil {
return err
}
defer f.Close()
opt := jpeg.Options{
Quality: 100,
}
err = jpeg.Encode(f, dst, &opt)
if err != nil {
return err
}
return nil
}