2

I am using a C library (tesseract) from my Python code. The C library has print statements like these that show up in my python program's stdout:

tprintf("Too few characters. Skipping this page\n");

Is there a way to suppress these print statements when using the C library from python? I've tried an approach I found on SO but it doesn't help. It does suppress print "test", however, it doesn't suppress the output coming from tprintf in the C library.

I've tried the solution mention here but it doesn't work. I still see output on the stdout.

devnull = open('/dev/null', 'w')
oldstdout_fno = os.dup(sys.stdout.fileno())
os.dup2(devnull.fileno(), 1)

tesseract = ctypes.cdll.LoadLibrary("/path/to/tesseract.so")
api = tesseract.TessBaseAPICreate()
rc = tesseract.TessBaseAPIInit3(api, TESSDATA_PREFIX, "osd")
tesseract.TessBaseAPISetPageSegMode(api, 1) #this line prints "Too few characters. Skipping this page" to stdout

os.dup2(oldstdout_fno, 1)
Community
  • 1
  • 1
Adam
  • 109
  • 1
  • 6
  • This looks relevant: https://sourceforge.net/p/tesseract-ocr/discussion/534361/thread/e95165c1/ (srderr, not stdout, and the option of specifying output to a file instead) – DavidW Apr 20 '16 at 06:41
  • Can you set Tesseract variable, such as: `api.setVariable("debug_file", "/dev/null");` See https://github.com/tesseract-ocr/tesseract/wiki/FAQ. – nguyenq Apr 22 '16 at 14:50

0 Answers0