当前位置: 首页 > 图灵资讯 > 行业资讯> python如何获取验证码图片

python如何获取验证码图片

发布时间:2025-04-06 15:58:07

首先导入一些使用的库:re、Image、pytesseract、selenium、time。

importre#用于正则
fromPILimportImage#用于打开图片和处理图片
importpytesseract#用于将图片转换为文本
fromseleniumimportwebdriver#用于打开网站
importtime#停止代码运行

为了进一步识别,需要获取验证码图片。

定义webdriver和find_element_by_selector方法,用于打开网页和定位验证码图片的元素。

classVerificationCode:
def__init__(self):
self.driver=webdriver.Firefox()
self.find_element=self.driver.find_element_by_css_selector

然后打开浏览器截取验证码图片

defget_pictures(self):
self.driver.get('http:/123.25.123.3')#打开登录页面
self.driver.save_screenshot('pictures.png')#全屏截图
page_snap_obj=Image.open('pictures.png')
img=self.find_element('#pic')#验证码元素的位置
time.sleep(1)
location=img.location
size=img.size#获取验证码的大小参数
left=location['x']
top=location['y']
right=left+size['width']
bottom=top+size['height']
image_obj=page_snap_obj.crop((left,top,right,bottom))#根据验证码的长宽,切割验证码
image_obj.show()#打开切割后的完整验证码
self.driver.close()#处理验证码后关闭浏览器
returnimage_obj

未处理前的验证码图片如下:

对于python来说,未处理的验证码图片识别率较低。仔细看,可以发现图片中有一些点扰乱了彩色识别,对识别率影响很大。

相关推荐:Python视频教程

下面处理获得的验证码。

首先,用convert将图片变成黑白。设置threshold阈值,超过阈值的为黑色。

defprocessing_image(self):
image_obj=self.get_pictures()#获取验证码
img=image_obj.convert("L")#转灰度
pixdata=img.load()
w,h=img.size
threshold=160#该阈值不适用于所有验证码。请根据验证码设置具体阈值
#黑色遍历所有像素,大于阈值
foryinrange(h):
forxinrange(w):
ifpixdata[x,y]<threshold:
pixdata[x,y]=0
else:
pixdata[x,y]=255
returnimg

灰度处理后的图片:

然后删除一些扰乱识别的像素。

defdelete_spot(self):
images=self.processing_image()
data=images.getdata()
w,h=images.size
black_point=0
forxinrange(1,w-1):
foryinrange(1,h-1):
mid_pixel=data[w*y+x]#中央像素点像素值
ifmid_pixel<50:#找出上下左右四个方向的像素点像素值
top_pixel=data[w*(y-1)+x]
left_pixel=data[w*y+(x-1)]
down_pixel=data[w*(y+1)+x]
right_pixel=data[w*y+(x+1)]
#判断上下左右黑色像素点总数
iftop_pixel<10:
black_point+=1
ifleft_pixel<10:
black_point+=1
ifdown_pixel<10:
black_point+=1
ifright_pixel<10:
black_point+=1
ifblack_point<1:
images.putpixel((x,y),255)
black_point=0
#images.show()
returnimages

去除噪声后的图片:

最后,将处理后的图片转换为文本。

首先设置pyteseract路径,因为默认路径是错误的,然后将图片转换为文本。由于个别图片中的识别会出现处理遗漏,会被识别为空格或点或分号,因此增加了去除验证码中特殊字符的处理。

defimage_str(self):
image=self.delete_spot()
pytesseract.pytesseract.tesseract_cmd=r"C:\ProgramFiles\Tesseract-OCR\tesseract.exe"#pyteseract路径设置
result=pytesseract.image_to_string(image)#图片转文字
resultj=re.sub(u"(\u4e00-\u9fa5\u0030-\u0039\u0041-\u005a\u0061-\u007a))","",result)#去除特殊字符识别的特殊字符
result_four=resultj[0:4]#只获得前四个字符
#print(resultj)#打印识别验证码
returnresult_four

完整代码如下:

importre#用于正则
fromPILimportImage#用于打开图片和处理图片
importpytesseract#用于将图片转换为文本
fromseleniumimportwebdriver#用于打开网站
importtime#停止代码运行

classVerificationCode:
def__init__(self):
self.driver=webdriver.Firefox()
self.find_element=self.driver.find_element_by_css_selector

defget_pictures(self):
self.driver.get('http:/123.25.123.3')#打开登录页面
self.driver.save_screenshot('pictures.png')#全屏截图
page_snap_obj=Image.open('pictures.png')
img=self.find_element('#pic')#验证码元素的位置
time.sleep(1)
location=img.location
size=img.size#获取验证码的大小参数
left=location['x']
top=location['y']
right=left+size['width']
bottom=top+size['height']
image_obj=page_snap_obj.crop((left,top,right,bottom))#根据验证码的长宽,切割验证码
image_obj.show()#打开切割后的完整验证码
self.driver.close()#处理验证码后关闭浏览器
returnimage_obj

defprocessing_image(self):
image_obj=self.get_pictures()#获取验证码
img=image_obj.convert("L")#转灰度
pixdata=img.load()
w,h=img.size
threshold=160
#遍历所有像素,黑色大于阈值
foryinrange(h):
forxinrange(w):
ifpixdata[x,y]<threshold:
pixdata[x,y]=0
else:
pixdata[x,y]=255
returnimg

defdelete_spot(self):
images=self.processing_image()
data=images.getdata()
w,h=images.size
black_point=0
forxinrange(1,w-1):
foryinrange(1,h-1):
mid_pixel=data[w*y+x]#中央像素像素值
ifmid_pixel<50:#找出上下左右四个方向像素点像素值
top_pixel=data[w*(y-1)+x]
left_pixel=data[w*y+(x-1)]
down_pixel=data[w*(y+1)+x]
right_pixel=data[w*y+(x+1)]
#判断上下左右黑色像素点总数
iftop_pixel<10:
black_point+=1
ifleft_pixel<10:
black_point+=1
ifdown_pixel<10:
black_point+=1
ifright_pixel<10:
black_point+=1
ifblack_point<1:
images.putpixel((x,y),255)
black_point=0
#images.show()
returnimages

defimage_str(self):
image=self.delete_spot()
pytesseract.pytesseract.tesseract_cmd=r"C:\ProgramFiles\Tesseract-OCR\tesseract.exe"
#pyteseract路径设置
result=pytesseract.image_to_string(image)#图片转文字
resultj=re.sub(u"(\u4e00-\u9fa5\u0030-\u0039\u0041-\u005a\u0061-\u007a))","",result)
#去除特殊字符识别的特殊字符
result_four=resultj[0:4]#只获得前四个字符
#print(resultj)#打印识别验证码
returnresult_four

if__name__='__main__':
a=VerificationCode()
a.image_str()

相关文章

python3兼容python2吗

python3兼容python2吗

2025-05-09
python3 whl怎么安装

python3 whl怎么安装

2025-05-09
python 字典怎么提取value

python 字典怎么提取value

2025-05-09
python 怎样计算字符串的长度

python 怎样计算字符串的长度

2025-05-09
python 怎么样反向输出字符串

python 怎么样反向输出字符串

2025-05-09
python 怎么判断字符串开头

python 怎么判断字符串开头

2025-05-09