forked from zikes/chrisify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
43 lines (36 loc) · 893 Bytes
/
utils.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
package main
import (
"image"
"image/draw"
"log"
"os"
)
func rectMargin(pct float64, rect image.Rectangle) image.Rectangle {
width := float64(rect.Max.X - rect.Min.X)
height := float64(rect.Max.Y - rect.Min.Y)
padding_width := int(pct * (width / 100) / 2)
padding_height := int(pct * (height / 100) / 2)
return image.Rect(
rect.Min.X-padding_width,
rect.Min.Y-padding_height*3,
rect.Max.X+padding_width,
rect.Max.Y+padding_height,
)
}
func loadImage(file string) image.Image {
reader, err := os.Open(file)
if err != nil {
log.Fatalf("error loading %s: %s", file, err)
}
img, _, err := image.Decode(reader)
if err != nil {
log.Fatalf("error loading %s: %s", file, err)
}
return img
}
func canvasFromImage(i image.Image) *image.RGBA {
bounds := i.Bounds()
canvas := image.NewRGBA(bounds)
draw.Draw(canvas, bounds, i, bounds.Min, draw.Src)
return canvas
}