这是我的代码:
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()
对于第一个输出,我得到了我需要的字母D。
D
这是预期的结果,但当它尝试执行第二个打印语句时,会输出以下内容。
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, in image_to_osd
}[output_type]()
File "C:\Users\Me\Documents\Python\OpenCV\OpenCV_WokringTest\venv\lib\site-packages\pytesseract\pytesseract.py", line 401, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "C:\Users\Me\Documents\Python\OpenCV\OpenCV_WokringTest\venv\lib\site-packages\pytesseract\pytesseract.py", line 218, in run_and_get_output
run_tesseract(**kwargs)
File "C:\Users\Me\Documents\Python\OpenCV\OpenCV_WokringTest\venv\lib\site-packages\pytesseract\pytesseract.py", line 194, in run_tesseract
raise TesseractError(status_code, get_errors(error_string))
pytesseract.pytesseract.TesseractError: (1, 'Tesseract Open Source OCR Engine v4.0.0.20181030 with Leptonica Warning: Invalid resolution 0 dpi. Using 70 instead. Too few characters. Skipping this page Warning. Invalid resolution 0 dpi. Using 70 instead. Too few characters. Skipping this page Error during processing.').
我不确定该怎么办。在网上找不到太多关于这个错误的信息。同时,我也不知道该怎么做。目标只是为了输出我的信件的方向。提前感谢所有有帮助的评论!
original
)传递给image_to_osd
而不是未经处理的图像(Image.open('D_Testing.png')
),会发生什么? - Dmitrii Z.--dpi
到config
中解决了它。image = Image.open(path) config_str = '--dpi ' + str(image.info['dpi'][0]) text = pytesseract.image_to_string(image , config=config_str)
- mbauer