From 81d2ad3e792a329c71d9a8eab2df4109b5c84a6a Mon Sep 17 00:00:00 2001 From: Mrs4s <1844812067@qq.com> Date: Sun, 2 Aug 2020 13:50:09 +0800 Subject: [PATCH] update ascii art. --- global/art.go | 101 -------------------------------------------------- go.mod | 3 +- go.sum | 9 +++-- main.go | 10 ++--- 4 files changed, 13 insertions(+), 110 deletions(-) delete mode 100644 global/art.go diff --git a/global/art.go b/global/art.go deleted file mode 100644 index 617065c..0000000 --- a/global/art.go +++ /dev/null @@ -1,101 +0,0 @@ -package global - -import ( - "bytes" - "fmt" - "golang.org/x/image/font" - "golang.org/x/image/font/basicfont" - "golang.org/x/image/math/fixed" - "image" - "image/color" - _ "image/jpeg" - _ "image/png" - "io" -) - -// https://github.com/xrlin/AsciiArt - -func Convert(f io.Reader, chars []string, subWidth, subHeight int, imageSwitch bool, bgColor, penColor color.RGBA) (string, *image.NRGBA, error) { - var charsLength = len(chars) - if charsLength == 0 { - return "", nil, fmt.Errorf("no chars provided") - } - if subWidth == 0 || subHeight == 0 { - return "", nil, fmt.Errorf("subWidth and subHeight params is required") - } - m, _, err := image.Decode(f) - if err != nil { - return "", nil, err - } - imageWidth, imageHeight := m.Bounds().Max.X, m.Bounds().Max.Y - var img *image.NRGBA - if imageSwitch { - img = initImage(imageWidth, imageHeight, bgColor) - } - piecesX, piecesY := imageWidth/subWidth, imageHeight/subHeight - var buff bytes.Buffer - for y := 0; y < piecesY; y++ { - offsetY := y * subHeight - for x := 0; x < piecesX; x++ { - offsetX := x * subWidth - averageBrightness := calculateAverageBrightness(m, image.Rect(offsetX, offsetY, offsetX+subWidth, offsetY+subHeight)) - char := getCharByBrightness(chars, averageBrightness) - buff.WriteString(char) - if img != nil { - addCharToImage(img, char, x*subWidth, y*subHeight, penColor) - } - } - buff.WriteString("\n") - } - return buff.String(), img, nil -} - -func initImage(width, height int, bgColor color.RGBA) *image.NRGBA { - img := image.NewNRGBA(image.Rect(0, 0, width, height)) - for x := 0; x < width; x++ { - for y := 0; y < height; y++ { - img.Set(x, y, bgColor) - } - } - return img -} -func calculateAverageBrightness(img image.Image, rect image.Rectangle) float64 { - var averageBrightness float64 - width, height := rect.Max.X-rect.Min.X, rect.Max.Y-rect.Min.Y - var brightness float64 - for x := rect.Min.X; x < rect.Max.X; x++ { - for y := rect.Min.Y; y < rect.Max.Y; y++ { - r, g, b, _ := img.At(x, y).RGBA() - brightness = float64(r>>8+g>>8+b>>8) / 3 - averageBrightness += brightness - } - } - averageBrightness /= float64(width * height) - return averageBrightness -} - -func getCharByBrightness(chars []string, brightness float64) string { - index := int(brightness*float64(len(chars))) >> 8 - if index == len(chars) { - index-- - } - return chars[len(chars)-index-1] -} - -func addCharToImage(img *image.NRGBA, char string, x, y int, penColor color.RGBA) { - face := basicfont.Face7x13 - point := fixed.Point26_6{X: fixed.Int26_6(x * 64), Y: fixed.Int26_6(y * 64)} - d := &font.Drawer{ - Dst: img, - Src: image.NewUniform(penColor), - Face: face, - Dot: point, - } - d.DrawString(char) -} - -var Colors = map[string]color.RGBA{"black": {0, 0, 0, 255}, - "gray": {140, 140, 140, 255}, - "red": {255, 0, 0, 255}, - "green": {0, 128, 0, 255}, - "blue": {0, 0, 255, 255}} diff --git a/go.mod b/go.mod index 509493d..4c2c7c1 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/Mrs4s/go-cqhttp go 1.14 require ( - github.com/Mrs4s/MiraiGo v0.0.0-20200802003417-9cd1355853c7 + github.com/Mrs4s/MiraiGo v0.0.0-20200802045511-04aad9705bdc github.com/gin-gonic/gin v1.6.3 github.com/gorilla/websocket v1.4.2 github.com/guonaihong/gout v0.1.1 @@ -14,6 +14,7 @@ require ( github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 github.com/tidwall/gjson v1.6.0 github.com/xujiajun/nutsdb v0.5.0 + github.com/yinghau76/go-ascii-art v0.0.0-20190517192627-e7f465a30189 golang.org/x/image v0.0.0-20200618115811-c13761719519 golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect diff --git a/go.sum b/go.sum index e76b792..e035d47 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/Mrs4s/MiraiGo v0.0.0-20200802003417-9cd1355853c7 h1:El3V+TFChR7AdfzMa8YqVIc/HX6lp5/4gwgMix9pz7w= -github.com/Mrs4s/MiraiGo v0.0.0-20200802003417-9cd1355853c7/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU= +github.com/Mrs4s/MiraiGo v0.0.0-20200802045511-04aad9705bdc h1:mtPgcLy1VNr+nTA7vQibBYpYpFALJ+ChVkUwA8KNKfI= +github.com/Mrs4s/MiraiGo v0.0.0-20200802045511-04aad9705bdc/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU= github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0= github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= @@ -65,6 +65,8 @@ github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHX github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -97,9 +99,10 @@ github.com/xujiajun/nutsdb v0.5.0 h1:j/jM3Zw7Chg8WK7bAcKR0Xr7Mal47U1oJAMgySfDn9E github.com/xujiajun/nutsdb v0.5.0/go.mod h1:owdwN0tW084RxEodABLbO7h4Z2s9WiAjZGZFhRh0/1Q= github.com/xujiajun/utils v0.0.0-20190123093513-8bf096c4f53b h1:jKG9OiL4T4xQN3IUrhUpc1tG+HfDXppkgVcrAiiaI/0= github.com/xujiajun/utils v0.0.0-20190123093513-8bf096c4f53b/go.mod h1:AZd87GYJlUzl82Yab2kTjx1EyXSQCAfZDhpTo1SQC4k= +github.com/yinghau76/go-ascii-art v0.0.0-20190517192627-e7f465a30189 h1:4UJw9if55Fu3HOwbfcaQlJ27p3oeJU2JZqoeT3ITJQk= +github.com/yinghau76/go-ascii-art v0.0.0-20190517192627-e7f465a30189/go.mod h1:rIrm5geMiBhPQkdfUm8gDFi/WiHneOp1i9KjmJqc+9I= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/image v0.0.0-20200618115811-c13761719519 h1:1e2ufUJNM3lCHEY5jIgac/7UTjd6cgJNdatjPdFWf34= golang.org/x/image v0.0.0-20200618115811-c13761719519/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= diff --git a/main.go b/main.go index f05039a..273b5a0 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,8 @@ import ( rotatelogs "github.com/lestrrat-go/file-rotatelogs" log "github.com/sirupsen/logrus" easy "github.com/t-tomalak/logrus-easy-formatter" - "image/color" + asciiart "github.com/yinghau76/go-ascii-art" + "image" "io" "io/ioutil" "os" @@ -141,10 +142,9 @@ func main() { if !rsp.Success { switch rsp.Error { case client.NeedCaptcha: - art, _, err := global.Convert(bytes.NewReader(rsp.CaptchaImage), []string{" ", "1", "i", ":", "*", "|", "."}, 1, 1, false, global.Colors["gray"], color.RGBA{}) - global.Check(err) - fmt.Println(art) - log.Warn("请输入验证码.") + img, _, _ := image.Decode(bytes.NewReader(rsp.CaptchaImage)) + fmt.Println(asciiart.New("image", img).Art) + log.Warn("请输入验证码: (回车提交)") text, _ := console.ReadString('\n') rsp, err = cli.SubmitCaptcha(strings.ReplaceAll(text, "\n", ""), rsp.CaptchaSign) continue