一、环境准备
1、安装pillow/pytesseract
pip install pillow
pip install pytesseract
2、安装Tesseract-OCR.exe
官网下载地址:
https://github.com/UB-Mannheim/tesseract/wiki
3、修改路径
F:\Python3.9\Lib\site-packages\pytesseract 找到pytesseract.py文件:修改tesseract_cmd路径为Tesseract-OCR.exe的安装路径
4、安装cv2
手动下载cv2包,解压后将cv2.pyd放到site_packages的目录下,下载地址:
https://pypi.python.org/pypi/cv2-wrapper/0.1
或者直接命令安装:
pip install opencv-python
二、简单代码实现
1、获取验证码图片
def get_code_image(self, image1, image2):
try:
img_ele = self.get_element(LPL.code_img, doc="获取验证码图片")
left = img_ele.location.get('x')
upper = img_ele.location.get('y')
right = left + img_ele.size.get('width')
lower = upper + img_ele.size.get('height')
img = Image.open(image1)
new_img = img.crop((left, upper, right, lower))
new_img.save(image2)
print(left, right, upper, lower)
except:
print("找不到验证码元素")
2、处理验证码图片
def handle_code_img(self, image2, image3):
image = Image.open(image2)
image = image.convert('L')
sharpness = ImageEnhance.Contrast(image)
sharp_img = sharpness.enhance(2.0)
sharp_img.save(image3)
numcode = Image.open(image3)
vcode = pytesseract.image_to_string(numcode, lang='eng',
config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789').strip()
return vcode
|