5行Python实现验证码识别

最近发现一个新库,试用一下发现非常实用,特意今天分享给大家。

该库名也是非常有趣 —— ddddocr(谐音带带弟弟OCR)

环境要求

python >= 3.8
Windows/Linux/Macox..

可以通过以下命令安装

pip install ddddocr

参数说明

 

 测试图片

测试代码

 

import ddddocr

ocr = ddddocr.DdddOcr()
with open('1.jpg', 'rb') as f:
    img_bytes = f.read()
res = ocr.classification(img_bytes)

print(res)

 

 

自写验证码 

只是会破解不行我们这次来个彻底的,上手做一个。原理很简单就是简单的随机函数和PIL库的使用

from PIL import Image,ImageDraw,ImageFont
import random
def getRandomColor():
   r = random.randint(0, 255)
   g = random.randint(0, 255)
   b = random.randint(0, 255)
   return (r,g,b)
def getRandomStr():
   num_random = str(random.randint(1,9))
   random_upper_alpha = chr(random.randint(65,90))
   random_char = random.choice([num_random,random_upper_alpha])
   return random_char
image = Image.new('RGB',(120,40),(255,255,255))
draw = ImageDraw.Draw(image)
font = ImageFont.truetype(r'K:\msyh.ttc',size=24)
for i in range(4):
   draw.text((10+i*30,10),getRandomStr(),getRandomColor(),font=font)
width = 120
height = 40
for i in range(5):
   x1 = random.randint(0,width)
   x2 = random.randint(0,width)
   y1 = random.randint(0,height)
   y2 = random.randint(0,height)
   draw.line((x1,x2,y1,y2),fill=getRandomColor())
for i in range(20):
   draw.point([random.randint(0,width),random.randint(0,height)],fill=getRandomColor())
   x = random.randint(0,width)
   y = random.randint(0,height)
   draw.arc((x,y,x+5,y+5),0,90,fill=getRandomColor())
image.save('feixue.jpg')

 

  自己尝试一下可不可识别吧

 

posted @ 2021-09-07 20:42  流年中渲染了微笑  阅读(688)  评论(0编辑  收藏  举报