/
imggo.go
102 lines (97 loc) · 2.27 KB
/
imggo.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"os"
"fmt"
"strings"
"strconv"
"github.com/codegangsta/cli"
"github.com/fatih/color"
"github.com/disintegration/imaging"
)
func main() {
app := cli.NewApp()
app.Author = "zujko"
app.Name = "img-go"
app.Usage = "INSERT USAGE HERE"
app.Commands = []cli.Command{
{
Name: "resize",
Aliases: []string{"r"},
Usage: "Resizes the image",
Action: func(c *cli.Context) {
if(len(c.Args()) >= 2) {
img, err := imaging.Open(c.Args()[0])
if err != nil {
color.Red("Error opening image")
} else {
size := strings.Split(c.Args()[1],"x")
width,we := strconv.Atoi(size[0])
height,he := strconv.Atoi(size[1])
if (we != nil || he != nil) {
color.Red("Invalid dimension arguments")
return
}
if(width > 1000 || height > 1000) {
color.Red("Dimensions are too large")
return
}
resized := imaging.Thumbnail(img, width, height, imaging.CatmullRom)
resizeName := "resized.jpg"
if len(c.Args()) == 3 {
resizeName = c.Args()[2]
}
err := imaging.Save(resized,resizeName)
if err != nil {
color.Red("Error saving image")
} else {
color.Green("Image resized")
}
}
} else {
fmt.Println("usage: image_name size [output_name]")
}
},
},
{
Name: "blur",
Aliases: []string{"bl"},
Usage: "Adds a blur to the image",
Action: func(c *cli.Context) {
if len(c.Args()) < 2 {
fmt.Println("usage: image_name blur_amount [output_name]")
} else {
img, err := imaging.Open(c.Args()[0])
if err != nil {
color.Red("Error opening Image")
return
}
blurAmt,er := strconv.ParseFloat(c.Args()[1], 64)
if er != nil {
color.Red("Invalid arguments")
return
}
dstImage := imaging.Blur(img, blurAmt)
imgName := "blur.jpg"
if len(c.Args()) == 3 {
imgName = c.Args()[2]
}
saveErr := imaging.Save(dstImage, imgName)
if saveErr != nil {
color.Red("Error saving image")
} else {
color.Green("Image blurred")
}
}
},
},
{
Name: "brighten",
Aliases: []string{"br"},
Usage: "Brighten or darken the image",
Action: func(c *cli.Context) {
//BRIGHTEN IMAGE
},
},
}
app.Run(os.Args)
}