/
main.go
88 lines (71 loc) · 1.96 KB
/
main.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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/esimov/caire"
"github.com/xyproto/carveimg"
"github.com/xyproto/vt100"
)
const versionString = "carve 1.4.0"
func main() {
if len(os.Args) <= 1 {
fmt.Println(versionString)
fmt.Fprintln(os.Stderr, "Please supply a GIF, JPEG, PNG or WEBP image filename")
os.Exit(1)
}
filename := os.Args[1]
// Initialize vt100 terminal settings
vt100.Init()
// Prepare a canvas
c := vt100.NewCanvas()
// Find the width and height of the canvas
width := int(c.Width())
height := int(c.Height())
// Prepare the content-aware image resizing
p := &caire.Processor{
BlurRadius: 1, // or ie. 4
SobelThreshold: 4, // or ie. 2
NewWidth: width,
NewHeight: height,
Percentage: false,
Square: false,
Debug: false,
//Preview: true, // Show GUI window, may cause errors related to context or ctx.Px
FaceDetect: true,
//FaceAngle: 0.0,
//MaskPath: "",
//RMaskPath: "",
ShapeType: "circle",
//SeamColor: "#ff0000",
}
// Load the given filename
nImage, err := carveimg.LoadImage(filename)
if err != nil {
vt100.Close()
fmt.Fprintf(os.Stderr, "Could not load %s: %s\n", filename, err)
os.Exit(1)
}
// Content aware resizing
resizedImage, err := p.Resize(nImage)
if err != nil {
vt100.Close()
fmt.Fprintf(os.Stderr, "Could not resize image: %s\n", err)
os.Exit(1)
}
// Draw the image to the canvas, using only the basic 16 colors
if err := carveimg.Draw(c, resizedImage); err != nil {
vt100.Close()
fmt.Fprintf(os.Stderr, "Could not draw image: %s\n", err)
os.Exit(1)
}
// Output the filename on top of the image
baseFilename := filepath.Base(filename)
c.Write(uint((width-len(baseFilename))/2), 0, vt100.LightBlue, vt100.BackgroundDefault, baseFilename)
// Draw the contents of the canvas to the screen
c.Draw()
// Wait for a keypress
vt100.WaitForKey()
// Reset the vt100 terminal settings
vt100.Close()
}