[{"data":1,"prerenderedAt":527},["ShallowReactive",2],{"tag-ocr":3},[4],{"_path":5,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":9,"description":10,"date":11,"layout":12,"subtitle":13,"image":14,"optimized_image":14,"category":15,"tags":16,"author":19,"paginate":7,"body":20,"_type":521,"_id":522,"_source":523,"_file":524,"_stem":525,"_extension":526},"/posts/playing-with-ocr-libraries","posts",false,"","Playing with OCR libraries","How to recognize text from images with pytesseract library. From image to python string","2020-11-22T11:00:00.000Z","post","How to recognize text from images with pytesseract library","/assets/img/uploads/ocr.jpg","code",[17,18],"ocr","python","jaimedearcos",{"type":21,"children":22,"toc":516},"root",[23,40,47,70,103,108,177,182,224,230,478,490,496,510],{"type":24,"tag":25,"props":26,"children":27},"element","p",{},[28,31,38],{"type":29,"value":30},"text","A Few days ago I found a cool library for text recognition in images, ",{"type":24,"tag":32,"props":33,"children":35},"a",{"href":34},"https://pypi.org/project/pytesseract/",[36],{"type":29,"value":37},"pytesseract",{"type":29,"value":39},".\nWith this library and just a few lines of code you can implement OCR features.",{"type":24,"tag":41,"props":42,"children":44},"h2",{"id":43},"get-started",[45],{"type":29,"value":46},"Get started",{"type":24,"tag":25,"props":48,"children":49},{},[50,52,58,60,68],{"type":29,"value":51},"Install ",{"type":24,"tag":15,"props":53,"children":55},{"className":54},[],[56],{"type":29,"value":57},"teseract",{"type":29,"value":59}," in MacOs (",{"type":24,"tag":32,"props":61,"children":65},{"href":62,"rel":63},"https://tesseract-ocr.github.io/tessdoc/Home.html",[64],"nofollow",[66],{"type":29,"value":67},"for other platforms",{"type":29,"value":69},")",{"type":24,"tag":71,"props":72,"children":76},"pre",{"className":73,"code":74,"language":75,"meta":8,"style":8},"language-sh shiki shiki-themes github-dark github-light","brew install tesseract\n","sh",[77],{"type":24,"tag":15,"props":78,"children":79},{"__ignoreMap":8},[80],{"type":24,"tag":81,"props":82,"children":85},"span",{"class":83,"line":84},"line",1,[86,92,98],{"type":24,"tag":81,"props":87,"children":89},{"style":88},"--shiki-default:#B392F0;--shiki-light:#6F42C1",[90],{"type":29,"value":91},"brew",{"type":24,"tag":81,"props":93,"children":95},{"style":94},"--shiki-default:#9ECBFF;--shiki-light:#032F62",[96],{"type":29,"value":97}," install",{"type":24,"tag":81,"props":99,"children":100},{"style":94},[101],{"type":29,"value":102}," tesseract\n",{"type":24,"tag":25,"props":104,"children":105},{},[106],{"type":29,"value":107},"Create a new python environment & install requirements",{"type":24,"tag":71,"props":109,"children":113},{"className":110,"code":111,"language":112,"meta":8,"style":8},"language-bash shiki shiki-themes github-dark github-light","python -m venv en\nsource env/bin/activate\npip install -r requirements.txt\n","bash",[114],{"type":24,"tag":15,"props":115,"children":116},{"__ignoreMap":8},[117,140,154],{"type":24,"tag":81,"props":118,"children":119},{"class":83,"line":84},[120,124,130,135],{"type":24,"tag":81,"props":121,"children":122},{"style":88},[123],{"type":29,"value":18},{"type":24,"tag":81,"props":125,"children":127},{"style":126},"--shiki-default:#79B8FF;--shiki-light:#005CC5",[128],{"type":29,"value":129}," -m",{"type":24,"tag":81,"props":131,"children":132},{"style":94},[133],{"type":29,"value":134}," venv",{"type":24,"tag":81,"props":136,"children":137},{"style":94},[138],{"type":29,"value":139}," en\n",{"type":24,"tag":81,"props":141,"children":143},{"class":83,"line":142},2,[144,149],{"type":24,"tag":81,"props":145,"children":146},{"style":126},[147],{"type":29,"value":148},"source",{"type":24,"tag":81,"props":150,"children":151},{"style":94},[152],{"type":29,"value":153}," env/bin/activate\n",{"type":24,"tag":81,"props":155,"children":157},{"class":83,"line":156},3,[158,163,167,172],{"type":24,"tag":81,"props":159,"children":160},{"style":88},[161],{"type":29,"value":162},"pip",{"type":24,"tag":81,"props":164,"children":165},{"style":94},[166],{"type":29,"value":97},{"type":24,"tag":81,"props":168,"children":169},{"style":126},[170],{"type":29,"value":171}," -r",{"type":24,"tag":81,"props":173,"children":174},{"style":94},[175],{"type":29,"value":176}," requirements.txt\n",{"type":24,"tag":25,"props":178,"children":179},{},[180],{"type":29,"value":181},"requirements.txt file",{"type":24,"tag":71,"props":183,"children":187},{"className":184,"code":185,"language":186,"meta":8,"style":8},"language-txt shiki shiki-themes github-dark github-light","numpy==1.19.4\nopencv-python==4.4.0.46\nPillow==8.0.1\npytesseract==0.3.6\n","txt",[188],{"type":24,"tag":15,"props":189,"children":190},{"__ignoreMap":8},[191,199,207,215],{"type":24,"tag":81,"props":192,"children":193},{"class":83,"line":84},[194],{"type":24,"tag":81,"props":195,"children":196},{},[197],{"type":29,"value":198},"numpy==1.19.4\n",{"type":24,"tag":81,"props":200,"children":201},{"class":83,"line":142},[202],{"type":24,"tag":81,"props":203,"children":204},{},[205],{"type":29,"value":206},"opencv-python==4.4.0.46\n",{"type":24,"tag":81,"props":208,"children":209},{"class":83,"line":156},[210],{"type":24,"tag":81,"props":211,"children":212},{},[213],{"type":29,"value":214},"Pillow==8.0.1\n",{"type":24,"tag":81,"props":216,"children":218},{"class":83,"line":217},4,[219],{"type":24,"tag":81,"props":220,"children":221},{},[222],{"type":29,"value":223},"pytesseract==0.3.6\n",{"type":24,"tag":41,"props":225,"children":227},{"id":226},"create-your-first-ocr-script",[228],{"type":29,"value":229},"Create your first ocr script",{"type":24,"tag":71,"props":231,"children":234},{"className":232,"code":233,"language":18,"meta":8,"style":8},"language-python shiki shiki-themes github-dark github-light","from PIL import Image\nimport pytesseract\nimport argparse\nimport sys\n\noriginal_image = \"image.jpg\"\nimage = cv2.imread(original_image)\ngray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n\n# Convert to grayscale for better performance\nfilename = \"grayscale_image.png\"\ncv2.imwrite(filename, gray)\n\n# Load the image as a PIL/Pillow image and apply OCR\ntext = pytesseract.image_to_string(Image.open(filename))\nos.remove(filename)\nprint(text)\n",[235],{"type":24,"tag":15,"props":236,"children":237},{"__ignoreMap":8},[238,263,276,288,300,310,329,347,375,383,393,411,420,428,437,455,464],{"type":24,"tag":81,"props":239,"children":240},{"class":83,"line":84},[241,247,252,257],{"type":24,"tag":81,"props":242,"children":244},{"style":243},"--shiki-default:#F97583;--shiki-light:#D73A49",[245],{"type":29,"value":246},"from",{"type":24,"tag":81,"props":248,"children":249},{"style":126},[250],{"type":29,"value":251}," PIL",{"type":24,"tag":81,"props":253,"children":254},{"style":243},[255],{"type":29,"value":256}," import",{"type":24,"tag":81,"props":258,"children":260},{"style":259},"--shiki-default:#E1E4E8;--shiki-light:#24292E",[261],{"type":29,"value":262}," Image\n",{"type":24,"tag":81,"props":264,"children":265},{"class":83,"line":142},[266,271],{"type":24,"tag":81,"props":267,"children":268},{"style":243},[269],{"type":29,"value":270},"import",{"type":24,"tag":81,"props":272,"children":273},{"style":259},[274],{"type":29,"value":275}," pytesseract\n",{"type":24,"tag":81,"props":277,"children":278},{"class":83,"line":156},[279,283],{"type":24,"tag":81,"props":280,"children":281},{"style":243},[282],{"type":29,"value":270},{"type":24,"tag":81,"props":284,"children":285},{"style":259},[286],{"type":29,"value":287}," argparse\n",{"type":24,"tag":81,"props":289,"children":290},{"class":83,"line":217},[291,295],{"type":24,"tag":81,"props":292,"children":293},{"style":243},[294],{"type":29,"value":270},{"type":24,"tag":81,"props":296,"children":297},{"style":259},[298],{"type":29,"value":299}," sys\n",{"type":24,"tag":81,"props":301,"children":303},{"class":83,"line":302},5,[304],{"type":24,"tag":81,"props":305,"children":307},{"emptyLinePlaceholder":306},true,[308],{"type":29,"value":309},"\n",{"type":24,"tag":81,"props":311,"children":313},{"class":83,"line":312},6,[314,319,324],{"type":24,"tag":81,"props":315,"children":316},{"style":259},[317],{"type":29,"value":318},"original_image ",{"type":24,"tag":81,"props":320,"children":321},{"style":243},[322],{"type":29,"value":323},"=",{"type":24,"tag":81,"props":325,"children":326},{"style":94},[327],{"type":29,"value":328}," \"image.jpg\"\n",{"type":24,"tag":81,"props":330,"children":332},{"class":83,"line":331},7,[333,338,342],{"type":24,"tag":81,"props":334,"children":335},{"style":259},[336],{"type":29,"value":337},"image ",{"type":24,"tag":81,"props":339,"children":340},{"style":243},[341],{"type":29,"value":323},{"type":24,"tag":81,"props":343,"children":344},{"style":259},[345],{"type":29,"value":346}," cv2.imread(original_image)\n",{"type":24,"tag":81,"props":348,"children":350},{"class":83,"line":349},8,[351,356,360,365,370],{"type":24,"tag":81,"props":352,"children":353},{"style":259},[354],{"type":29,"value":355},"gray ",{"type":24,"tag":81,"props":357,"children":358},{"style":243},[359],{"type":29,"value":323},{"type":24,"tag":81,"props":361,"children":362},{"style":259},[363],{"type":29,"value":364}," cv2.cvtColor(image, cv2.",{"type":24,"tag":81,"props":366,"children":367},{"style":126},[368],{"type":29,"value":369},"COLOR_BGR2GRAY",{"type":24,"tag":81,"props":371,"children":372},{"style":259},[373],{"type":29,"value":374},")\n",{"type":24,"tag":81,"props":376,"children":378},{"class":83,"line":377},9,[379],{"type":24,"tag":81,"props":380,"children":381},{"emptyLinePlaceholder":306},[382],{"type":29,"value":309},{"type":24,"tag":81,"props":384,"children":386},{"class":83,"line":385},10,[387],{"type":24,"tag":81,"props":388,"children":390},{"style":389},"--shiki-default:#6A737D;--shiki-light:#6A737D",[391],{"type":29,"value":392},"# Convert to grayscale for better performance\n",{"type":24,"tag":81,"props":394,"children":396},{"class":83,"line":395},11,[397,402,406],{"type":24,"tag":81,"props":398,"children":399},{"style":259},[400],{"type":29,"value":401},"filename ",{"type":24,"tag":81,"props":403,"children":404},{"style":243},[405],{"type":29,"value":323},{"type":24,"tag":81,"props":407,"children":408},{"style":94},[409],{"type":29,"value":410}," \"grayscale_image.png\"\n",{"type":24,"tag":81,"props":412,"children":414},{"class":83,"line":413},12,[415],{"type":24,"tag":81,"props":416,"children":417},{"style":259},[418],{"type":29,"value":419},"cv2.imwrite(filename, gray)\n",{"type":24,"tag":81,"props":421,"children":423},{"class":83,"line":422},13,[424],{"type":24,"tag":81,"props":425,"children":426},{"emptyLinePlaceholder":306},[427],{"type":29,"value":309},{"type":24,"tag":81,"props":429,"children":431},{"class":83,"line":430},14,[432],{"type":24,"tag":81,"props":433,"children":434},{"style":389},[435],{"type":29,"value":436},"# Load the image as a PIL/Pillow image and apply OCR\n",{"type":24,"tag":81,"props":438,"children":440},{"class":83,"line":439},15,[441,446,450],{"type":24,"tag":81,"props":442,"children":443},{"style":259},[444],{"type":29,"value":445},"text ",{"type":24,"tag":81,"props":447,"children":448},{"style":243},[449],{"type":29,"value":323},{"type":24,"tag":81,"props":451,"children":452},{"style":259},[453],{"type":29,"value":454}," pytesseract.image_to_string(Image.open(filename))\n",{"type":24,"tag":81,"props":456,"children":458},{"class":83,"line":457},16,[459],{"type":24,"tag":81,"props":460,"children":461},{"style":259},[462],{"type":29,"value":463},"os.remove(filename)\n",{"type":24,"tag":81,"props":465,"children":467},{"class":83,"line":466},17,[468,473],{"type":24,"tag":81,"props":469,"children":470},{"style":126},[471],{"type":29,"value":472},"print",{"type":24,"tag":81,"props":474,"children":475},{"style":259},[476],{"type":29,"value":477},"(text)\n",{"type":24,"tag":25,"props":479,"children":480},{},[481,483],{"type":29,"value":482},"You can find the complete code of this example in ",{"type":24,"tag":32,"props":484,"children":487},{"href":485,"rel":486},"https://github.com/JaimeDeArcos/ocr-python",[64],[488],{"type":29,"value":489},"this GitHub repository",{"type":24,"tag":41,"props":491,"children":493},{"id":492},"references",[494],{"type":29,"value":495},"References",{"type":24,"tag":497,"props":498,"children":499},"ul",{},[500],{"type":24,"tag":501,"props":502,"children":503},"li",{},[504],{"type":24,"tag":32,"props":505,"children":507},{"href":34,"rel":506},[64],[508],{"type":29,"value":509},"PyTesseract pip documentation",{"type":24,"tag":511,"props":512,"children":513},"style",{},[514],{"type":29,"value":515},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}",{"title":8,"searchDepth":142,"depth":142,"links":517},[518,519,520],{"id":43,"depth":142,"text":46},{"id":226,"depth":142,"text":229},{"id":492,"depth":142,"text":495},"markdown","content:posts:playing-with-ocr-libraries.md","content","posts/playing-with-ocr-libraries.md","posts/playing-with-ocr-libraries","md",1790510065090]