import math ,pytesseract ,cv2
from PIL import Image
class identifyText:
def __init__(self) -> None:
# 定义相似颜色的阈值,5~200之间为最佳值,5~500为有效值
self.threshold = 100
img_path = r'screen\screen.png'
new_img_path = r'screen\new_screen.png'
if self.transformedImage(img_path ,new_img_path):
print(self.characterRecognition(new_img_path))
# 计算两个颜色之间的欧几里得距离
def color_distance(self ,c1 ,c2):
# 如果图片没有透明通道则不需要传入和计算a通道
r1, g1, b1, a1 = c1
r2, g2, b2, a2 = c2
return math.sqrt((r1-r2)**2 + (g1-g2)**2 + (b1-b2)**2 + (a1-a2)**2)
# 转化图像为白底黑字,以提高识别准确性
def transformedImage(self ,img_path ,new_img_path):
# 打开原图
img = Image.open(img_path)
# 创建一个白色的背景图像
new_img = Image.new('RGBA', img.size, (255, 255, 255, 255))
# 遍历所有像素点
for x in range(img.width):
for y in range(img.hei
pytesseract文字识别,提高准确率的方法
于 2023-03-09 20:13:30 首次发布
该代码实现了一个Python类,用于识别图像中的文本。它首先通过欧几里得距离计算对图像进行颜色对比,转化为白底黑字,然后使用pytesseract库进行文字识别,主要针对数字进行识别。

最低0.47元/天 解锁文章
8220





