关于python处理图片验证码的处理

本文介绍了一种自动化处理登录页面数字验证码的方法,包括从界面截取验证码图片、图片降噪处理及利用pytesseract将图片验证码转换为文本的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前段时间在处理图片验证的问题,登陆窗口出现数字验证码,找了很多资料,请教前辈。到目前为止,有点眉目了,在这里分享一下:

        场景:自动化测试是在RF上面展开,处理验证码自然用到python,于是处理验证码的思路有了:从界面截取验证码图片,保存到本地->图片进行降噪处理->调用pytesseract里面的方法,把图片里面的验证码转换为文本。

        1)、从界面截取验证码图片,保存到本地:

def cut_pinnum_pic_from_page(xpath, window, pageScreen, pinNumPath):
    '''
    url='http://10.2.122.143:8000/?HIPPO_TRADE_URL=ws://10.2.122.143:1521#StockQuote'
    driver = webdriver.Chrome()
    driver.maximize_window()  #将浏览器最大化
    driver.get(url)
    driver.save_screenshot('C:\\aa.png')  #截取当前网页,该网页有我们需要的验证码
    '''
    driver = window
    #pageScreen = "C:\\yzm\\page.png"
    #pinNumPath = 'C:\\yzm\\pinnum.jpg'


    driver.save_screenshot(pageScreen)
    imgelement = driver.find_element_by_xpath(xpath)
    #获取验证码x,y轴坐标
    location = imgelement.location
    size=imgelement.size
    #写成我们需要截取的位置坐标
    rangle=(int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height']))
    i=Image.open(pageScreen)
    #使用Image的crop函数,截取验证码
    pin=i.crop(rangle)
    pin.save(pinNumPath)
    print ":::", pinNumPath, "saved successfully!"


    2)、图片进行降噪处理:


def getPixel(image, x, y, G, N):
    '''
    #二值判断,如果确认是噪声,用改点的上面一个点的灰度进行替换
    #该函数也可以改成RGB判断的,具体看需求如何
    '''
    L = image.getpixel((x, y))
    if L > G:
        L = True
    else:
        L = False


    nearDots = 0
    if L == (image.getpixel((x - 1, y - 1)) > G):
        nearDots += 1
    if L == (image.getpixel((x - 1, y)) > G):
        nearDots += 1
    if L == (image.getpixel((x - 1, y + 1)) > G):
        nearDots += 1
    if L == (image.getpixel((x, y - 1)) > G):
        nearDots += 1
    if L == (image.getpixel((x, y + 1)) > G):
        nearDots += 1
    if L == (image.getpixel((x + 1, y - 1)) > G):
        nearDots += 1
    if L == (image.getpixel((x + 1, y)) > G):
        nearDots += 1
    if L == (image.getpixel((x + 1, y + 1)) > G):
        nearDots += 1


    if nearDots < N:
        return image.getpixel((x, y-1))
    else:
        return None



def clearNoise(image, G, N, Z):
    '''
    降噪
    根据一个点A的RGB值,与周围的8个点的RBG值比较,设定一个值N(0 <N <8),当A的RGB值与周围8个点的RGB相等数小于N时,此点为噪点
    G: Integer 图像二值化阀值
    N: Integer 降噪率 0 <N <8
    Z: Integer 降噪次数
    @输出
    0:降噪成功
    1:降噪失败
    '''
    draw = ImageDraw.Draw(image)
    for i in xrange(0, Z):
        for x in xrange(1, image.size[0] - 1):
            for y in xrange(1, image.size[1] - 1):
                color = getPixel(image, x, y, G, N)
                if color != None:
                    draw.point((x, y), color)


def op_clearNoise(oldPic, newPic):
    '''
    执行去噪处理
    '''
    image = Image.open(oldPic)
    image = image.convert("L")
    clearNoise(image, 50, 4, 4)
    print ":::clearNoise successfully!"
    image.save(newPic)


    3)、调用pytesseract里面的方法,把图片里面的验证码转换为文本:

def image_to_string(imPath, cleanup):

    im = Image.open(imPath)
    try:
        util.image_to_scratch(im, scratch_image_name)
        call_tesseract(scratch_image_name, scratch_text_name_root)
        print ":::Begin to transfer string from image-", imPath
        text = util.retrieve_text(scratch_text_name_root)
    except Exception as e:
        print "::: Exception occured:------------"
        print e
    finally:
        if cleanup is True:
           util.perform_cleanup(scratch_image_name, scratch_text_name_root)
    return text.rstrip().lstrip()


转换后,可以针对识别的文本进行人为处理,比如有时候会把5识别成$,这时候可以自己写一段脚本进行转换,提高转换效率。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值