和代码:
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)
Run Code Online (Sandbox Code Playgroud)
还不错,但打印的结果,2 WW
不是正确的文字2HHH,那么如何删除那些黑点呢?
所以我试图从图像中提取文本.而且由于图像的质量和尺寸不佳,它会产生不准确的结果.我用PIL尝试了一些增强功能和其他功能,但这只会降低图像质量.
有人可以建议图像的一些增强,以获得更好的结果.几个图像的例子:
我使用pytesseractlib从图像中提取文本.当我在localhost上运行代码时,这工作正常.但是当我在openshift上部署时,会给我上面的错误.
下面是我到目前为止所编写的代码.
try:
import Image
except ImportError:
from PIL import Image
import pytesseract
filePath = PATH_WHERE_FILE_IS_LOCATED # '/var/lib/openshift/555.../app-root/data/data/y.jpg'
text = pytesseract.image_to_string(Image.open(filePath)) # this line produces error
Run Code Online (Sandbox Code Playgroud)
以上错误的追溯是
>>> pytesseract.image_to_string(Image.open(filePath))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/var/lib/openshift/56faaee42d527151d5000089/app- root/runtime/repo/pytesseract/pytesseract.py", line 132, in image_to_string
boxes=boxes)
File "/var/lib/openshift/56faaee42d527151d5000089/app-root/runtime/repo/pytesseract/pytesseract.py", line 73, in run_tesseract
stderr=subprocess.PIPE)
File "/opt/rh/python27/root/usr/lib64/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/opt/rh/python27/root/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or …Run Code Online (Sandbox Code Playgroud) 这是原始屏幕截图,我将图像裁剪成4个部分,并将图像的背景清除到我可以做的范围,但tesseract只检测到最后一列而忽略了其余部分.
显示tesseract的输出,因为在处理结果时我删除了空格
Femme—Fatale.
DaRkLoRdEIa
aChineseN1gg4
Noob_Diablo_
Run Code Online (Sandbox Code Playgroud)
显示tesseract的输出,因为在处理结果时我删除了空格
Kicked.
NosNoel
ChikiZD
Death_Eag|e_42
Chai—.
Run Code Online (Sandbox Code Playgroud)
3579 10 1 7 148
2962 3 O 7 101
2214 2 2 7 99
2205 1 3 6 78
Run Code Online (Sandbox Code Playgroud)
8212
7198
6307
5640
4884
15
40
40
6O
80
80
Run Code Online (Sandbox Code Playgroud)
我只是倾销输出
result = `pytesseract.image_to_string(Image.open("D:/newapproach/B&W"+str(i)+".jpg"),lang="New_Language")`
Run Code Online (Sandbox Code Playgroud)
但是我不知道如何从这里开始获得一致的结果.无论如何,我可以强制tesseract识别文本区域并使其扫描.因为在培训师(SunnyPage)中,tesseract默认识别扫描它无法识别某些区域,但一旦我选择手动,一切都被检测到并正确翻译成文本
我试图使用pytesseract库获取图像文件的字符位置.
import pytesseract
from PIL import Image
print pytesseract.image_to_string(Image.open('5.png'))
Run Code Online (Sandbox Code Playgroud)
是否有任何图书馆可以获得角色的每个位置
这是我的代码:
import pytesseract
import cv2
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe"
def main():
original = cv2.imread('D_Testing.png', 0)
# binary thresh it at value 100. It is now a black and white image
ret, original = cv2.threshold(original, 100, 255, cv2.THRESH_BINARY)
text = pytesseract.image_to_string(original, config='--psm 10')
print(text)
print(pytesseract.image_to_osd(Image.open('D_Testing.png')))
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
对于第一个输出,我得到了我需要的字母D
D
Run Code Online (Sandbox Code Playgroud)
这是有意的,但是当它尝试执行第二个打印语句时,它会吐出它。
Traceback (most recent call last):
File "C:/Users/Me/Documents/Python/OpenCV/OpenCV_WokringTest/PytesseractAttempt.py", line 18, in <module>
main()
File "C:/Users/Me/Documents/Python/OpenCV/OpenCV_WokringTest/PytesseractAttempt.py", line 14, in main
print(pytesseract.image_to_osd(Image.open('D_Testing.png')))
File "C:\Users\Me\Documents\Python\OpenCV\OpenCV_WokringTest\venv\lib\site-packages\pytesseract\pytesseract.py", line 402, …Run Code Online (Sandbox Code Playgroud) 这是我的问题,我想用pytesser来获取图片的内容.我的操作系统是Mac OS 10.11,我已经安装了PIL,pytesser,tesseract-ocr引擎和其他支持库,如libpng等.但是当我运行我的代码时,如下所示,会发生错误.
from pytesser import *
import os
image = Image.open('/Users/Grant/Desktop/1.png')
text = image_to_string(image)
print text
Run Code Online (Sandbox Code Playgroud)
接下来是错误消息
Traceback (most recent call last):
File "/Users/Grant/Documents/workspace/image_test/image_test.py", line 10, in <module>
text = image_to_string(im)
File "/Users/Grant/Documents/workspace/image_test/pytesser/pytesser.py", line 30, in image_to_string
call_tesseract(scratch_image_name, scratch_text_name_root)
File "/Users/Grant/Documents/workspace/image_test/pytesser/pytesser.py", line 21, in call_tesseract
retcode = subprocess.call(args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file …Run Code Online (Sandbox Code Playgroud) 有关将这些图像转换为文本的建议吗?我正在使用pytesseract,除了这个以外,它在大多数情况下都能很好地工作.理想情况下,我会完全阅读这些数字.最糟糕的情况我可以尝试使用PIL来确定'/'左边的数字是否为零.从左边开始,然后找到第一个白色像素
from PIL import Image
from pytesseract import image_to_string
myText = image_to_string(Image.open("tmp/test.jpg"),config='-psm 10')
myText = image_to_string(Image.open("tmp/test.jpg"))
Run Code Online (Sandbox Code Playgroud)
中间的斜线导致问题.我也尝试使用PIL的'.paste'在图像周围添加大量额外的黑色.我可能会尝试其他一些PIL技巧,但除非必须,否则我宁愿不去那条路.
我尝试使用config =' - psm 10',但我的8'有时会以":"和其他时间的随机字符出现.而且我的0都没有成功.
参考:pytesseract不适用于-psm 10的一位数图像
_____________EDIT_______________ 其他样本:
所以我正在做一些似乎现在正在工作的巫术转换.但看起来很容易出错:
def ConvertPPTextToReadableNumbers(text):
text = RemoveNonASCIICharacters(text)
text = text.replace("I]", "0")
text = text.replace("|]", "0")
text = text.replace("l]", "0")
text = text.replace("B", "8")
text = text.replace("D", "0")
text = text.replace("S", "5")
text = text.replace(".I'", "/")
text = text.replace(".I", "/")
text = text.replace("I'", "/")
text = text.replace("J", "/")
return …Run Code Online (Sandbox Code Playgroud) 我收到此错误:[WinError 2]只有当我使用pytesser进行OCR时,系统才能找到指定的文件.这是我的代码片段.
from PIL import Image
from pytesseract import *
image = Image.open('pranav.jpg')
print (image_to_string(image))****
Run Code Online (Sandbox Code Playgroud)
否则,当我使用PIL更改图像大小时,我不会收到此错误.
我想从 中的图像中提取文本python。为了做到这一点,我选择了pytesseract。当我尝试从图像中提取文本时,结果并不令人满意。我也经历了这个并实现了列出的所有技术。然而,它的表现似乎并不好。
图像:
代码:
import pytesseract
import cv2
import numpy as np
img = cv2.imread('D:\\wordsimg.png')
img = cv2.resize(img, None, fx=1.2, fy=1.2, interpolation=cv2.INTER_CUBIC)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
kernel = np.ones((1,1), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)
img = cv2.erode(img, kernel, iterations=1)
img = cv2.threshold(cv2.medianBlur(img, 3), 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
txt = pytesseract.image_to_string(img ,lang = 'eng')
txt = txt[:-1]
txt = txt.replace('\n',' ')
print(txt)
Run Code Online (Sandbox Code Playgroud)
输出:
t hose he large form might …Run Code Online (Sandbox Code Playgroud)