使用 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)

相关文章