import pytesseract
from PIL import Image,ImageEnhance
img = Image.open('C:\\Users\\NND\\Desktop\\1661932962331.jpg')
# 模式L”为灰色图像,它的每个像素用8个bit表示,0表示黑,255表示白,其他数字表示不同的灰度。
Img = img.convert('L')
Img.save("test1.png")
# 自定义灰度界限,大于这个值为黑色,小于这个值为白色
threshold = 140
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
# 图片二值化
photo = Img.point(table, '1')
photo.save("test1.png")
# 识别图片内容
img_path = 'test1.png'
text = pytesseract.image_to_string(Image.open(img_path),'eng')
print(text)
python图像二值化及图像识别
最新推荐文章于 2023-12-05 16:26:42 发布