从图像中提取文本

时间:2017-09-17 05:23:36

标签: python image-processing ocr tesseract python-tesseract

我正致力于从图像中提取文本。

最初图像以白色文字着色,在进一步处理图像时,文字显示为黑色,其他像素为白色(带有一些噪点),这里有一个样本:

现在当我尝试使用pytesseract(tesseract)进行OCR时,我仍然没有收到任何文字。

是否有可能从彩色图像中提取文本?

2 个答案:

答案 0 :(得分:5)

from PIL import Image
import pytesseract
import argparse
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path to the image")
args = vars(ap.parse_args())

# load the image and convert it to grayscale
image = cv2.imread(args["image"])
cv2.imshow("Original", image)

# Apply an "average" blur to the image

blurred = cv2.blur(image, (3,3))
cv2.imshow("Blurred_image", blurred)
img = Image.fromarray(blurred)
text = pytesseract.image_to_string(img, lang='eng')
print (text)
cv2.waitKey(0)

结果我得=“留在:Overwoter Bungalow $ 3»”

使用Contour并从中获取不必要的斑点怎么样?可能会工作

答案 1 :(得分:0)

尝试这个-

import os
from PIL import Image
import cv2
import pytesseract
import ftfy
import uuid

filename = 'uTGi5.png'
image = cv2.imread(os.path.join(filename))
gray = cv2.threshold(image, 200, 255, cv2.THRESH_BINARY)[1]
gray = cv2.resize(gray, (0, 0), fx=3, fy=3)
gray = cv2.medianBlur(gray, 9)
filename = str(uuid.uuid4())+".jpg"
cv2.imwrite(os.path.join(
    filename), gray)
config = ("-l eng --oem 3 --psm 11")
text = pytesseract.image_to_string(Image.open(os.path.join(
    filename)), config=config)
text = ftfy.fix_text(text)
text = ftfy.fix_encoding(text)
text = text.replace('-\n', '')
print(text)