This repository has been archived by the owner on Mar 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
change_image.go
63 lines (49 loc) · 1.48 KB
/
change_image.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
package imagework
import (
"fmt"
"image"
"path/filepath"
"strings"
"github.com/SteMak/sakura_bot/utils/strcont"
"github.com/anthonynsimon/bild/effect"
"github.com/anthonynsimon/bild/imgio"
"github.com/anthonynsimon/bild/segment"
"github.com/anthonynsimon/bild/transform"
"github.com/otiai10/gosseract"
)
// ConvertImage crop and convert image to left only captcha
func ConvertImage(messageTimeStamp string) {
img, err := imgio.Open(filepath.Join("sakura_images", messageTimeStamp+".png"))
if err != nil {
fmt.Println("ERROR opening file", err.Error())
return
}
img = transform.Crop(img, image.Rect(coordOfCode(img)))
img = effect.Invert(img)
img = effect.Grayscale(img)
img = segment.Threshold(img, 128)
err = imgio.Save(filepath.Join("sakura_images", "conv-"+messageTimeStamp+".jpg"), img, imgio.JPEGEncoder(100))
if err != nil {
fmt.Println("ERROR saving converted file", err.Error())
return
}
}
// ParseImage find codes in image
func ParseImage(messageTimeStamp string) (string, string) {
var err error
client := gosseract.NewClient()
err = client.SetImage(filepath.Join("sakura_images", "conv-"+messageTimeStamp+".jpg"))
if err != nil {
fmt.Println("ERROR opening converted file", err.Error())
}
text, err := client.Text()
if err != nil {
fmt.Println("ERROR reading text", err.Error())
}
client.Close()
codes := strings.Split(strcont.ClearStrange(strcont.ReplaceBadSymbols(text)), "/")
if len(codes) != 2 {
return text, ""
}
return codes[0], codes[1]
}