python解析图片验证码并登录

# !/usr/bin/python
# -*- coding: utf-8 -*-
"""
@author: chliang
@time: 2022/7/22 11:00
@describe: 识别图片字母和数字
"""
import os

import pytesseract
from PIL import Image
from collections import defaultdict
import base64
import re
import uuid

# 获取图片中像素点数量最多的像素
def get_threshold(image):
    pixel_dict = defaultdict(int)

    # 像素及该像素出现次数的字典
    rows, cols = image.size
    for i in range(rows):
        for j in range(cols):
            pixel = image.getpixel((i, j))
            pixel_dict[pixel] += 1

    count_max = max(pixel_dict.values())  # 获取像素出现出多的次数
    pixel_dict_reverse = {v: k for k, v in pixel_dict.items()}
    threshold = pixel_dict_reverse[count_max]  # 获取出现次数最多的像素点

    return threshold


# 按照阈值进行二值化处理
# threshold: 像素阈值
def get_bin_table(threshold):
    # 获取灰度转二值的映射table
    table = []
    for i in range(256):
        rate = 0.1  # 在threshold的适当范围内进行处理
        if threshold * (1 - rate) <= i <= threshold * (1 + rate):
            table.append(1)
        else:
            table.append(0)
    return table


# 去掉二值化处理后的图片中的噪声点
def cut_noise(image):

    rows, cols = image.size  # 图片的宽度和高度
    change_pos = []  # 记录噪声点位置

    # 遍历图片中的每个点,除掉边缘
    for i in range(1, rows - 1):
        for j in range(1, cols - 1):
            # pixel_set用来记录该店附近的黑色像素的数量
            pixel_set = []
            # 取该点的邻域为以该点为中心的九宫格
            for m in range(i - 1, i + 2):
                for n in range(j - 1, j + 2):
                    if image.getpixel((m, n)) != 1:  # 1为白色,0位黑色
                        pixel_set.append(image.getpixel((m, n)))

            # 如果该位置的九宫内的黑色数量小于等于4,则判断为噪声
            if len(pixel_set) <= 4:
                change_pos.append((i, j))

    # 对相应位置进行像素修改,将噪声处的像素置为1(白色)
    for pos in change_pos:
        image.putpixel(pos, 1)

    return image  # 返回修改后的图片


# 识别图片中的数字加字母
# 传入参数为图片路径,返回结果为:识别结果
def discern_image(img_path):

    image = Image.open(img_path)  # 打开图片文件
    imgry = image.convert('L')  # 转化为灰度图

    # 获取图片中的出现次数最多的像素,即为该图片的背景
    max_pixel = get_threshold(imgry)

    # 将图片进行二值化处理
    # 注意,是否使用二值化要看具体情况,有些图片二值化之后,可能关键信息会丢失,反而识别不出来
    table = get_bin_table(threshold=max_pixel)
    out = imgry.point(table, '1')

    # 去掉图片中的噪声(孤立点)
    out = cut_noise(out)

    # 保存图片
    # out.save('E://figures/img_gray.jpg')

    # 仅识别图片中的数字
    # text = pytesseract.image_to_string(out, config='digits')
    # 识别图片中的数字和字母
    text = pytesseract.image_to_string(out)

    # 去掉识别结果中的特殊字符
    exclude_char_list = ' .:\\|\'\"?![],()~@#$%^&*_+-={};<>/¥'
    text = ''.join([x for x in text if x not in exclude_char_list])
    return text

def ocr(source):
    # 1、信息提取
    result = re.search("data:image/(?P<ext>.*?);base64,(?P<data>.*)", source, re.DOTALL)
    if result:
        # ext = result.groupdict().get("ext")
        data = result.groupdict().get("data")
    else:
        raise Exception("Do not parse!")

    # 2、base64解码
    img = base64.urlsafe_b64decode(data)

    # 3、二进制文件保存
    filename = "{}.{}".format(uuid.uuid4(), 'png')
    with open(filename, "wb") as f:
        f.write(img)

    # img = cv2.imread('./'+filename)
    # imgtext = pytesseract.image_to_string(cv2.imread('./'+filename))
    imgepath = './'+filename
    imgtext = discern_image(imgepath)
    if os.path.exists(imgepath):
        os.remove(imgepath)
    return imgtext

if __name__ == '__main__':
   source = 'data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAeCAIAAABVOSykAAAAuElEQVR42u3YsQ3DIBBAUYbIXF4j\nK3gB9y6yiJUN3EUeJ00qy10Kw8Gd0BXAl36FEMUrDkSY5oOUBQjAAqtFrMfnKwVWLdaynVIDYSn3\nZ7Bir/31kxoLSxJUYt281vdTqjcsaT3pkvRqDEs53euxkuveNGB1OrNGxzLdhr3NrHqseDO3oeEJ\nqn9ntT3giy94E1ZxJ1jdYVkDyyuwXHLCyhzL519Z7W8HlsEOLP7gwQILLLAILLB8ugDQEuYGFO41\n5wAAAABJRU5ErkJggg=='
   r = ocr(source)
   print(r)
# !/usr/bin/python
# -*- coding: utf-8 -*-
"""
@author: chliang
@time: 2022/7/22 11:00
@describe: 
"""
import logging
import os

import requests
from imageocr import ocr

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

ROOT_URL = 'https://xxxx'
LOGIN_NAME = 'xxxx'
PASSWORD = os.getenv('password')

class Login:
    def __init__(self):
        self.num = 0
        self.HEADERS = {
            'Referer': 'https://xxxx',
            'User-Agent': 'xxxx'
        }

    def _get_image(self):
        r = requests.get(url='https://xxxx/getImage', headers=self.HEADERS)
        j = r.json()
        image = j['object']['image']
        img_text = ocr('data:image/gif;base64,'+image)
        return {'code': img_text.strip()[0:4], 'session_id': j['object']['sessionId']}

    def init_login(self):
        def req():
            token=''
            num = 0
            while (num < 10):
                v = self._get_image()
                logger.info(f"code={v['code']},session_id={v['session_id']}")
                self.HEADERS['Authorization'] = v['session_id']
                self.HEADERS['Content-Type'] = 'application/x-www-form-urlencoded'
                data = {
                    'loginName': LOGIN_NAME,
                    'password': PASSWORD,
                    'code': v['code'],
                    'JSESSIONID': v['session_id']
                }
                r = requests.post('https://xxxxxx/login', data=data, headers=self.HEADERS)
                j = r.json()
                if j['code'] == 1:
                    logger.info('------------< 登录成功 >--------------')
                    token = j['object']['token']
                    break
                num = num + 1
            logger.info(f'------------< token = {token} >-------------')
            return token

        try:
            return req()
        except Exception as e:
            logger.error(f'----------login error: {e}')
            return req()

if __name__ == '__main__':
    lo = Login().init_login()
    if len(lo) != 0:
        print(lo)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值