看一下简单的验证码长啥样

验证码长这个样子
1.png

为什么说验证码简单呢?

  1. 验证码位数固定(4位)
  2. 无任何干扰

我的识别思路

上文说了,该验证码4位数字固定的,而且数字在图片中的位置也是固定的
所以我的想法是

  1. 将图片转换为灰色
  2. 将图片分割为4等份
  3. 分别取出4张图片的rgb色值
  4. 和0-9的特征码进行对比(特征码就是rgb色值)

首先我们得先获取到0-9的rgb特征码

取出0-9的特征码

先引入PIL下的Image模块

from PIL import Image

然后打开一张图片,将图片分割为4等份

im = Image.open("1.png")
code_img_list = []
code_img_list.append(im.crop((2,0,8,10)))
code_img_list.append(im.crop((12,0,18,10)))
code_img_list.append(im.crop((22,0,28,10)))
code_img_list.append(im.crop((32,0,38,10)))

然后将这4张图片的rgb特征获取出来

 for code_img in code_img_list :
    num_data = []
    for i1 in range(0, code_img.size[1]):
        data = []
        for i in range(0, code_img.size[0]):
            data.append(str(code_img.load()[i,i1]))
        num_data.extend(data)
    code_tzm = "".join(num_data)
    print(code_tzm)

输出结果为:
1.png

为了方便,我已经把0-9的特征码获取出来了

    tzm_list = ["2302323232323023230230230230232323023023023023232302323230232323023232302323230232323023232302323230232323023023023023232302302302302323023232323230",
    "23023023230230230232323230230230230230232302302302302302323023023023023023230230230230230232302302302302302323023023023023023230230230230230232302302302323232323230",
    "230232323232302323023023023023232302302302302323023023023023023230230230230232302302302302323023023023023230230230230232302302302302323023023023023232323232323",
    "230232323232302323023023023023232302302302302323023023023023230230230232323023023023023023023230230230230230230232323023023023023232302302302302323023232323230",
    "23023023023230230230230230232302302302302323230230230232302323023023230230232302302323023023230230232323232323230230230232302302302302302323023023023023232323",
    "23232323232323230230230230230232302302302302302323023232323023232302302302323023023023023023230230230230230232323023023023023232302302302302323023232323230",
    "230230232323230230232302302302323230230230230230232302302302302302323023232323023232302302302323230230230230232323023023023023232302302302302323023232323230",
    "2323232323232323023023023230232302302302323023023023023230230230230230232302302302302323023023023023023230230230230230232302302302302302323023023023023023230230230",
    "2302323232323023230230230230232323023023023023232302302302302323023232323230230232302302323023230230230230232323023023023023232302302302302323023232323230",
   "2302323232302302323023023023230232302302302302323230230230230232323023023023232302323232302323023023023023023230230230230230232323023023023230230232323230230"]

tzm_list的index就是想对于的数字特征码

完整代码

from PIL import Image
from io import BytesIO

def check_code(img_data):
    code = ""
    tzm_list = ["2302323232323023230230230230232323023023023023232302323230232323023232302323230232323023232302323230232323023023023023232302302302302323023232323230",
"23023023230230230232323230230230230230232302302302302302323023023023023023230230230230230232302302302302302323023023023023023230230230230230232302302302323232323230",
"230232323232302323023023023023232302302302302323023023023023023230230230230232302302302302323023023023023230230230230232302302302302323023023023023232323232323",
"230232323232302323023023023023232302302302302323023023023023230230230232323023023023023023023230230230230230230232323023023023023232302302302302323023232323230",
"23023023023230230230230230232302302302302323230230230232302323023023230230232302302323023023230230232323232323230230230232302302302302302323023023023023232323",
"23232323232323230230230230230232302302302302302323023232323023232302302302323023023023023023230230230230230232323023023023023232302302302302323023232323230",
"230230232323230230232302302302323230230230230230232302302302302302323023232323023232302302302323230230230230232323023023023023232302302302302323023232323230",
"2323232323232323023023023230232302302302323023023023023230230230230230232302302302302323023023023023023230230230230230232302302302302302323023023023023023230230230",
"2302323232323023230230230230232323023023023023232302302302302323023232323230230232302302323023230230230230232323023023023023232302302302302323023232323230",
"2302323232302302323023023023230232302302302302323230230230230232323023023023232302323232302323023023023023023230230230230230232323023023023230230232323230230"]

    im = Image.open(BytesIO(img_data)).convert("L")

    code_img_list = []
    code_img_list.append(im.crop((2,0,8,10)))
    code_img_list.append(im.crop((12,0,18,10)))
    code_img_list.append(im.crop((22,0,28,10)))
    code_img_list.append(im.crop((32,0,38,10)))

    for code_img in code_img_list :
        num_data = []
        for i1 in range(0, code_img.size[1]):
            data = []
            for i in range(0, code_img.size[0]):
                data.append(str(code_img.load()[i,i1]))
            num_data.extend(data)
        code_tzm = "".join(num_data)
        print(code_tzm)
        code += str(tzm_list.index(code_tzm))

    return code

开始
最近在学习golang,今早看见一篇文章
发现这个算法挺有意思,于是有此文

什么是睡眠排序
字面意思解释就是将每个数字sleep当前值时间然后输出
数字越大,时间越长
即可达到数字越小输出越早

代码


import (
    "fmt"
    "time"
)


func main() {
    var iList []int = []int{1,35,22,13,26,75,89,241}
    for i:=0; i < len(iList);i++{
        go tosleep(iList[i])
    }
    time.Sleep(time.Second * 200)
}

func tosleep(data int){
    time.Sleep(time.Second * time.Duration(data))
    fmt.Println(data)
}

输出结果
1
13
22
26
35
75
89
241