如何使用裁剪功能使这个图像对pytesseract可读?

时间:2017-05-26 20:17:27

标签: python python-imaging-library tesseract

所以这是以下图片http://imgur.com/a/E6mpB

我想隔离这些号码中的每一个,以便我可以用pytesseract阅读它们。

这是我直到现在写的脚本:

try:
    import Image
except ImportError:
    from PIL import Image
import pytesseract as tes

filename = "test.png";

x1 = (50,60,220,110)
x2 = (45,160,230,228)
x3 = (45,260,330,328)

image = Image.open( filename );

cropped_image = 'cropped_image'
scores = [x1, x2, x3]
for i in scores:
    cropped_image1 = image.crop ( i )
    results = tes.image_to_string(cropped_image1,lang="letsgodigital",boxes=False)
    print(results)

所以基本上我是为每个数字裁剪这个图像。但是我不觉得这个解决方案非常有效(再加上找不到每个数字的坐标都不是那么容易。我计划将这个脚本应用于多个图像,但数字总是如此在同一个地方。有关如何使这个脚本变得更好的任何想法吗?

希望问题清楚。

感谢。

P.S:我不知道为什么,但直接在我的帖子中上传图片并不起作用....

1 个答案:

答案 0 :(得分:0)

如果每个图像的编号都在样本位置,使用multiprocessing处理裁剪的图像会加快运行时间,因为multiprocessing可以执行数据并行操作以传递一个图像(数据) CPU核心。如果您的机器有一个带有4个内核的CPU,则样品的处理时间将缩短到3个单位以上,而不是12个单位的12个裁剪图像。您可以在其上添加Google以获取更多信息和示例。

要提高pytesseract OCR准确度,您可以将图像阈值设置为黑白。下面是样本裁剪图像。

enter image description here

黑白色的示例代码。

threshold = 60  
cropped = image.crop(x)
cropped = cropped.convert('L')
inverted = PIL.ImageOps.invert(cropped)
inverted_mask = inverted.point(lambda p: p < threshold and 255)