How to pass OpenCV image to Tesseract in python? <p>Given Python code invoking Tesseract`s C API and using <strong>ctypes</strong> library, in the <em>Option #1</em> image is being loaded by Tesseract and it works fine! The problem is in the <em>Option #2</em>, when I try to pass image loaded by OpenCV the Tesseract returns garbage:</p> <pre><code>from ctypes import * import cv2 class API(Structure): _fields_ = [] lang = "eng" ts = cdll.LoadLibrary("c:/Tesseract-OCR/libtesseract302.dll") ts.TessBaseAPICreate.restype = POINTER(API) api = ts.TessBaseAPICreate() rc = ts.TessBaseAPIInit3(api, 'c:/Tesseract-OCR/', lang) ##### Option #1 out = ts.TessBaseAPIProcessPages(api, 'c:/Tesseract-OCR/doc/eurotext.tif', None, 0) print 'Option #1 =&gt; ' + string_at(out) ##### Option #2 #TESS_API void TESS_CALL TessBaseAPISetImage(TessBaseAPI* handle, const unsigned char* imagedata, int width, int height, # int bytes_per_pixel, int bytes_per_line); im = cv2.imread('c:/Temp/Downloads/test-slim/eurotext.jpg', cv2.COLOR_BGR2GRAY) c_ubyte_p = POINTER(c_ubyte) ##ts.TessBaseAPISetImage.argtypes = [POINTER(API), c_ubyte_p, c_int, c_int, c_int, c_int] ts.TessBaseAPISetImage(api, im.ctypes.data_as(c_ubyte_p), 800, 1024, 3, 800 * 3) out = ts.TessBaseAPIGetUTF8Text(api) print 'Option #2 =&gt; ' + string_at(out) </code></pre> <p>and output is as follows:</p> <blockquote> <p>Option #1 => The (quick) [brown] {fox} jumps! Over the $43,456.78 #90 dog &amp; duck/goose, as 12.5% of E-mail from aspammer@website.com is spam. Der ,,schnelleâ€� braune Fuchs springt ï¬�ber den faulen Hund. Le renard brun Â«rapideÂ» saute par-dessus le chien paresseux. La volpe marrone rapida salta sopra il cane pigro. El zorro marrÃ©n rÃ©pido salta sobre el perro perezoso. A raposa marrom rzipida salta sobre o cï¬�o preguicoso.</p> <p>Option #2 => 7?:5:*:>\â€”â€˜- ;2â€”;i3E:?:;i3".i: iiâ€˜; 3;â€™ f-iÃ©%<em>:::â€™::;?:=Â«â€™:: =Â£&lt;:7â€˜iÂ§5.&lt; :â€”'\â€”;:=Ã©:â€™â€”..=.:a,';2â€™:3â€˜</em>:3_3:l.':â€”â€˜:â€”:Â£â‚¬:-_â€™:Â§3;;%Â§%ai5~Â«:Ã©::3%iaÂ»â‚¬E:</p> </blockquote> <p>Remarks:</p> <ul> <li>I tried python-tesseract and tightocr libraries, which are good<br> enough, but lacking documentation</li> <li>here I use opencv.imread in order to have possibility to apply math algorithms on matrix</li> </ul> <p>Any ideas how to pass OpenCV image (which is numpy.ndarray) to Tesseract? Any help would be useful.</p>