python 图片验证码降噪、识别
生活随笔
收集整理的這篇文章主要介紹了
python 图片验证码降噪、识别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
from PIL import Image
import pytesseract
import os'''
安裝 tesseract-ocr-setup-3.05.01.exe
下面的環境變量根據自己電腦實際情況配置
'''
os.environ['TESSDATA_PREFIX'] = 'F:\\safe-soft\\ocr\\Tesseract-OCR\\tessdata'
pytesseract.pytesseract.tesseract_cmd = 'F:\\safe-soft\\ocr\\Tesseract-OCR\\tesseract.exe'# 取像素點中間值
def median(img, x, y):L = []xl = [x - 1, x, x + 1]yl = [y - 1, y, y + 1]for i in xl:for j in yl:gray = img.getpixel((i, j)) # 取出灰度值L.append(gray)L.sort()c = L[4]return c# 對圖片進行降噪處理
def denoise(path):img1 = Image.open(path) # 圖像1img1 = img1.convert('L') # 將圖像1轉換為灰度圖w, h = img1.sizeimg2 = Image.new('L', (w, h), 'white') # 圖像2for x in range(1, w - 1):for y in range(1, h - 1):c = median(img1, x, y) # 求中值img2.putpixel((x, y), c) # 將灰度設置為中值return img2# 解析圖片驗證碼
def getYzm(imgPath):text = pytesseract.image_to_string(denoise(imgPath))return textprint(getYzm('D:/img-test/3.png'))
總結
以上是生活随笔為你收集整理的python 图片验证码降噪、识别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 心灵鸡汤 - tenth (10th)
- 下一篇: Ubuntu14.04的常用快捷键