python随记

滑块验证码

2020-05-05  本文已影响0人  LCSan
#encoding=utf-8
'''
Created on 2020年1月6日

@author: 瞌睡蟲子
'''
import PIL.ImageChops as imagechops
from PIL import Image, ImageDraw
import random

def CalcDistance(srcImg,distImg,picwidth=50):
    im1 = Image.open(srcImg)
    im2 = Image.open(distImg)
    #得出两图不一致的地方
    diff=imagechops.difference(im1, im2)
    draw =ImageDraw.Draw(diff)
    sz=im1.size
    #通过颜色处理清除干扰块
    for x in range(0,sz[0]):
        for y in range(0,sz[1]):
            pixelColor=diff.getpixel((x,y))
            if pixelColor[0]>=100 or pixelColor[1]>=100 or pixelColor[2]>=100:
                draw.line((x, y, x, y),(255,255,255,255))
            else:
                draw.line((x, y, x, y),(0,0,0,0))
    # #清理完可以show一下,查看清理完之后的黑白化效果
#     diff.show()
#     diff.save(r'C:\Users\Administrator\Desktop\我们不一样.png')
    #找第一个块中的参照点
    firsetPoint=[0,0]
    for x in range(0,sz[0]):
        for y in range(0,sz[1]):
            pixelColor=diff.getpixel((x,y))
            if (len(pixelColor)==4 and pixelColor!=(0,0,0,0)) or (len(pixelColor)==3 and pixelColor!=(0,0,0)):
                firsetPoint=[x,y]
                break
        if firsetPoint!=[0,0]:
            break
    # 往后跳50找第二个块中的参照点,50是矩形宽度
#     print(firsetPoint)
    secondPoint=[0,0]
    for x in range(firsetPoint[0]+picwidth,260):
        for y in range(0,sz[1]):
            pixelColor=diff.getpixel((x,y))
            if (len(pixelColor)==4 and pixelColor!=(0,0,0,0)) or (len(pixelColor)==3 and pixelColor!=(0,0,0)):
                secondPoint=[x,y]
                break
        if secondPoint!=[0,0]:
            break
    # #画两条线看看位置是否标注正确,仅用于调试
#     draw.line((firsetPoint[0], firsetPoint[1], firsetPoint[0]+20, firsetPoint[1]),(6,255,9,0))
#     draw.line((secondPoint[0], secondPoint[1], secondPoint[0]+20, secondPoint[1]),(9,8,255,0))
#     diff.save(r'C:\Users\Administrator\Desktop\我们不一样1.png')
    # diff.show()
#     print(secondPoint)
    diffPixel=secondPoint[0]-firsetPoint[0]
#     print(diffPixel)
    return GetStacks(diffPixel)
    
def GetStacks(distance):
    distance += 20
    '''
    匀加速\减速运行
        v = v0 + a * t
    位移:
    s = v * t + 0.5 * a * (t**2)
    '''
    # 初速度
    v0 = 0
    # 加减速度列表
    a_list = [3, 4, 5]
    # 时间
    t = 0.2
    # 初始位置
    s = 0
    # 向前滑动轨迹
    forward_stacks = []
    mid = distance * 3 / 5
    while s < distance:
        if s < mid:
            a = a_list[random.randint(0, 2)]
        else:
            a = -a_list[random.randint(0, 2)]
        v = v0
        stack = v * t + 0.5 * a * (t ** 2)
        # 每次拿到的位移
        stack = round(stack)
        s += stack
        v0 = v + a * t
        forward_stacks.append(stack)
    back_stacks = [-1, -1, -2, -3, -2, -3, -2, -2, -3, -1]
    return {'forward_stacks': forward_stacks, 'back_stacks': back_stacks}

if __name__ == '__main__':
    data=CalcDistance(r"C:\Users\Administrator\Desktop\11.bmp", r"C:\Users\Administrator\Desktop\12.bmp")
    print(data)
上一篇 下一篇

猜你喜欢

热点阅读