Python Tesseract不转换某些图像
我编写了python代码来将一些图像转换为字符串。我有一些png格式的手机号码图片。 但我只把一个转换成文本,其他的没有转换 这是我的密码:Python Tesseract不转换某些图像,python,tesseract,Python,Tesseract,我编写了python代码来将一些图像转换为字符串。我有一些png格式的手机号码图片。 但我只把一个转换成文本,其他的没有转换 这是我的密码: try: from PIL import Image except ImportError: import Image import pytesseract import os THIS_FOLDER = os.path.dirname(os.path.abspath(__file__)) my_file = os.path.join(T
try:
from PIL import Image
except ImportError:
import Image
import pytesseract
import os
THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
my_file = os.path.join(THIS_FOLDER, 'images/3564.jpg')
def ocr_core(filename):
"""
This function will handle the core OCR processing of images.
"""
text = pytesseract.image_to_string(Image.open(filename)) # We'll use Pillow's Image class to open the image and pytesseract to detect the string in the image
return text
for x in range(24):
number = ocr_core('/Users/evilslab/Documents/Websites/www.futurepoint.dev.cc/dobuyme/python/images/'+str(x)+'.png')
print ("The number is "+number)
我有24张图片,我得到了第9张图片的值
此图像正在工作:
这不起作用
为什么会出现这种情况?有时图像需要一些工作来提高其质量 请参见Tesseract Wiki: 在您的示例中,我只需将图像大小调整至少120%即可获得数字
from PIL import Image
import pytesseract
import os
folder = os.path.dirname(os.path.abspath(__file__))
def ocr_core(filename):
image = Image.open(filename)
w, h = image.size
#image = image.resize((int(w*1.2), int(h*1.2))) # 120%
image = image.resize((w*2, h*2)) # 200%
#text = pytesseract.image_to_string(image, config='-c tessedit_char_whitelist="0123456789+"')
text = pytesseract.image_to_string(image)
text = text.replace(' ', '')
return text
for filename in os.listdir(folder):
if filename.endswith('.png'):
number = ocr_core(os.path.join(folder, filename))
print("number:", number)
编辑:当我使用选项
pms=7
时,即使不调整大小,它也能识别数字,这意味着“将图像视为单个文本行。”
(请参阅)
你得到了什么-空字符串或错误的数字或字母而不是数字?把它拆了<代码>“不工作”是最无用的信息。tesseract有许多选项可以更改它的工作方式。如果我将第二个图像大小调整200%,那么识别数字就没有问题。在tesseract Wiki上阅读:当我将200%放入文本中时,我得到了空字符串。为什么?模块
Pyteseract
(和程序tesseract
)可能会使用不同的方法来识别文本,它有许多选项,这些选项可能决定它的工作方式,但可能需要一些工作和时间。图像太小不好,图像太大也不好,图像模糊也可能会有问题,数字太近也可能会有问题-您可能需要测试不同的选项并更改图像以获得最佳效果更好的效果。pytesseract
使用深度网络识别图像,可以学习新字符,但我从未尝试过。好的。我有一个疑问,我制作了一个抓取脚本来拍摄手机图像,有很多图像。在命令行或浏览器中运行脚本更好?即使使用cron
或其他工具,也可以运行命令行中的脚本调度程序,这样即使在我们睡觉的时候也可以运行。但有时页面使用太多的JavaScript,可能需要使用Selenium(用于控制web浏览器)。Selenium在服务器中运行
text = pytesseract.image_to_string(image, config='--psm 7')