使用 pytesseract OCR 从图像中识别文本 [英] Use pytesseract OCR to recognize text from an image

查看:45
本文介绍了使用 pytesseract OCR 从图像中识别文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 Pytesseract 从这张图片中提取文本:

I need to use Pytesseract to extract text from this picture:

和代码:

from PIL import Image, ImageEnhance, ImageFilter
import pytesseract
path = 'pic.gif'
img = Image.open(path)
img = img.convert('RGBA')
pix = img.load()
for y in range(img.size[1]):
    for x in range(img.size[0]):
        if pix[x, y][0] < 102 or pix[x, y][1] < 102 or pix[x, y][2] < 102:
            pix[x, y] = (0, 0, 0, 255)
        else:
            pix[x, y] = (255, 255, 255, 255)
img.save('temp.jpg')
text = pytesseract.image_to_string(Image.open('temp.jpg'))
# os.remove('temp.jpg')
print(text)

和temp.jpg"是

and the "temp.jpg" is

还不错,但是打印出来的结果是,2 WW不是正确的文本2HHH,那么我该如何去除那些黑点?

Not bad, but the result of print is ,2 WW Not the right text2HHH, so how can I remove those black dots?

推荐答案

这是我的解决方案:

import pytesseract
from PIL import Image, ImageEnhance, ImageFilter

im = Image.open("temp.jpg") # the second one 
im = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(2)
im = im.convert('1')
im.save('temp2.jpg')
text = pytesseract.image_to_string(Image.open('temp2.jpg'))
print(text)

这篇关于使用 pytesseract OCR 从图像中识别文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆