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) # 图像1
img1 = img1.convert('L') # 将图像1转换为灰度图
w, h = img1.size
img2 = Image.new('L', (w, h), 'white') # 图像2
for 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 text
print(getYzm('D:/img-test/3.png'))
python 图片验证码降噪、识别
最新推荐文章于 2023-12-12 17:18:04 发布