python验证码识别实例代码

yipeiwu_com5年前Python基础

本文研究的主要是Python验证码识别的相关代码,具体如下。

Talk is cheap, show you the Code!

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from PIL import Image

#打开图像
im=np.array(Image.open('yzm.png'))

#得到图像3个维度
h,w,san=im.shape

X=[(h-x,y) for x in range(h) for y in range (w) if im[x][y][2]<200]

#将X转换成numpy的array类型,方便后续运算操作
X=np.array(X)

n_clusters=4
k_means=KMeans(init='k-means++',n_clusters=n_clusters)
k_means.fit(X)

k_means_labels=k_means.labels_
k_means_cluster_centers=k_means.cluster_centers_
k_means_labels_unique=np.unique(k_means_labels)

colors=['#4EACC5','#FF9C34','#4E9A06','#FF3300']
plt.figure()
plt.hold(True)
for k,col in zip(range(n_clusters),colors):
 my_members=k_means_labels==k
 cluster_center=k_means_cluster_centers[k]
 plt.plot(X[my_members,1],X[my_members,0],'w',markerfacecolor=col,marker='.')
 plt.plot(cluster_center[1],cluster_center[0],'o',markerfacecolor=col,markeredgecolor='k',markersize=6)

plt.title('KMeans')
plt.grid(True)
plt.show()

总结

以上就是本文关于python验证码识别实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

Python中的包和模块实例

一、实例和结果 1)实例的结构和具体的文件: 复制代码 代码如下: PyPackage │  PyCommonM.py │  __init__.py │ ├─p1Pa...

Python使用jsonpath-rw模块处理Json对象操作示例

本文实例讲述了Python使用jsonpath-rw模块处理Json对象操作。分享给大家供大家参考,具体如下: 这两天在写一个爬虫,需要从网站返回的json数据提取一些有用的数据。 向u...

Python中的字符串替换操作示例

字符串的替换(interpolation), 可以使用string.Template, 也可以使用标准字符串的拼接. string.Template标示替换的字符, 使用"$"符号, 或...

python虚拟环境virtualenv的使用教程

virtualenv 是一个创建隔绝的Python环境的工具。virtualenv创建一个包含所有必要的可执行文件的文件夹,用来使用Python工程所需的包。 安装 pip inst...

python实现将内容分行输出

#python版一行内容分行输出   a="aA1一bB2二cC3三dD4四eE5五fF6六gG7七hH8八iI9九" """ 分行输出为: abcdefghi ABCD...