{"id":79752,"date":"2020-08-28T19:54:44","date_gmt":"2020-08-28T14:24:44","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=79752"},"modified":"2026-06-03T14:55:20","modified_gmt":"2026-06-03T09:25:20","slug":"extract-text-from-image-with-python-opencv","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/extract-text-from-image-with-python-opencv\/","title":{"rendered":"Extract Text from Image with Python &amp; OpenCV"},"content":{"rendered":"<p>Python will automatically find and extract text from an image. Yes, Python can do amazing things. Let&#8217;s start working on this interesting Python project.<\/p>\n<blockquote><p>A picture is worth a thousand words<\/p><\/blockquote>\n<p>You must have heard the quote many times right! Well, the saying is very true because sometimes the picture says it all.<\/p>\n<p>We have discussed the importance of a single picture so far and now it\u2019s time to learn something more interesting about it. In this python project, we will learn how to extract text content from images using openCV and tesseract. Now let\u2019s see how we can create this.<\/p>\n<h3>What is Tesseract?<\/h3>\n<p>It is an open-source engine for optical character recognition (OCR). It efficiently reads text from images and is very easy to use. As mentioned earlier it is open source so it is free to use.<\/p>\n<h3>Prerequisites<\/h3>\n<p>To implement this project you should have basic knowledge of:<\/p>\n<ol>\n<li>Python<\/li>\n<li>Tessaract<\/li>\n<li>OpenCV.<\/li>\n<li>Tkinter<\/li>\n<\/ol>\n<h3>Download Project Code<\/h3>\n<p>Before proceeding ahead, please download the source code of Text Extraction Project: <a href=\"https:\/\/drive.google.com\/file\/d\/1VEKVCoQyns15c15L7aUiJR5pMfKEH3zg\/view?usp=drive_link\"><strong>Extract Text from Image with Python<\/strong><\/a>.<\/p>\n<h2>Steps to start text extraction<\/h2>\n<p>Let&#8217;s start the text detection and extraction project development<\/p>\n<h3>Install required libraries<\/h3>\n<p>To install the libraries use pip installer from the command prompt \/ terminal:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Pip install opencv-python\r\nPip install pytesseract\r\npip install tkinter\r\n<\/pre>\n<h3>Create main.py<\/h3>\n<p>Create main.py file and add the following code<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from tkinter import *\r\nfrom tkinter import filedialog\r\nfrom PIL import ImageTk, Image\r\nimport cv2\r\nimport pytesseract \r\n\r\npytesseract.pytesseract.tesseract_cmd = 'C:\\\\Program Files (x86)\\\\Tesseract-OCR\\\\tesseract.exe'\r\n\r\nroot = Tk()\r\nroot.title('TechVidvan Text from image project') \r\n\r\nnewline= Label(root)\r\nuploaded_img=Label(root)\r\nscrollbar = Scrollbar(root)\r\nscrollbar.pack( side = RIGHT, fill = Y )\r\n\r\n\r\ndef extract(path):\r\n    Actual_image = cv2.imread(path)\r\n    Sample_img = cv2.resize(Actual_image,(400,350))\r\n    Image_ht,Image_wd,Image_thickness = Sample_img.shape\r\n    Sample_img = cv2.cvtColor(Sample_img,cv2.COLOR_BGR2RGB)\r\n    texts = pytesseract.image_to_data(Sample_img) \r\n    mytext=\"\"\r\n    prevy=0\r\n    for cnt,text in enumerate(texts.splitlines()):\r\n        if cnt==0:\r\n            continue\r\n        text = text.split()\r\n        if len(text)==12:\r\n            x,y,w,h = int(text[6]),int(text[7]),int(text[8]),int(text[9])\r\n            if(len(mytext)==0):\r\n                prey=y\r\n            if(prevy-y&gt;=10 or y-prevy&gt;=10):\r\n                print(mytext)\r\n                Label(root,text=mytext,font=('Times',15,'bold')).pack()\r\n                mytext=\"\"\r\n            mytext = mytext + text[11]+\" \"\r\n            prevy=y\r\n    Label(root,text=mytext,font=('Times',15,'bold')).pack()\r\n\r\ndef show_extract_button(path):\r\n    extractBtn= Button(root,text=\"Extract text\",command=lambda: extract(path),bg=\"#2f2f77\",fg=\"gray\",pady=15,padx=15,font=('Times',15,'bold'))\r\n    extractBtn.pack()\r\n\r\ndef upload():\r\n    try:\r\n        path=filedialog.askopenfilename()\r\n        image=Image.open(path)\r\n        img=ImageTk.PhotoImage(image)\r\n        uploaded_img.configure(image=img)\r\n        uploaded_img.image=img\r\n        show_extract_button(path)\r\n    except:\r\n        pass  \r\n\r\nuploadbtn = Button(root,text=\"Upload an image\",command=upload,bg=\"#2f2f77\",fg=\"gray\",height=2,width=20,font=('Times',15,'bold')).pack()\r\nnewline.configure(text='\\n')\r\nnewline.pack()\r\nuploaded_img.pack()\r\n\r\nroot.mainloop()\r\n<\/pre>\n<p><strong>Explanation:<\/strong><\/p>\n<ol>\n<li>Import all the required libraries (opencv, tkinter, tesseract)<\/li>\n<li>Provide the location of the tesseract.exe file.<\/li>\n<li>Tkinter provides GUI functionalities: open an image dialog box so user can upload an image<\/li>\n<li>Let&#8217;s jump to the extract function which takes the path of the image as a parameter\n<ul>\n<li>In this function, we\u2019ll read the image using cv2.imread. We will also resize the image so that we can get well-formatted output for all different sizes of input images.<\/li>\n<li>Tesseract works on RGB images and opencv reads an image as BGR image, so we need to convert the image and then call tesseract functions on the image. Here,the conversion is done using cv2.cvtCOLOR().<\/li>\n<li>we have stored height, width, and thickness of the input image using img.shape for later use.<\/li>\n<li>After the pre-processing, call image_to_data() function of <a href=\"https:\/\/github.com\/tesseract-ocr\/tesseract\">tesseract<\/a> which returns a string (of extracted text from the image0.<\/li>\n<li>Print the whole string for better understanding.<\/li>\n<li>The string is a multiline string, where each line contains extracted text but its first line (starting from zero) contains headings that are not useful for us, so we will skip the very first line.<\/li>\n<li>Now, split the string to get the extracted text and finally print the extracted text on the screen.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3>Project Output<\/h3>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/08\/text-extraction-project.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-79753\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/08\/text-extraction-project.png\" alt=\"text extraction project\" width=\"869\" height=\"511\" \/><\/a><\/p>\n<h2>Summary<\/h2>\n<p>In this article, we have successfully developed a project which automatically detects and extracts text from images very efficiently using inbuilt functions of pytesseract and opencv.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python will automatically find and extract text from an image. Yes, Python can do amazing things. Let&#8217;s start working on this interesting Python project. A picture is worth a thousand words You must have&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":79754,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[210],"tags":[3243,204,483],"class_list":["post-79752","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-machine-learning","tag-extract-text-from-image","tag-machine-learning-project","tag-python-project"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Extract Text from Image with Python &amp; OpenCV - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn how to automatically detect and extract text content from image using Python. In this project we will use python libraries openCV and tesseract.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/extract-text-from-image-with-python-opencv\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Extract Text from Image with Python &amp; OpenCV - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn how to automatically detect and extract text content from image using Python. In this project we will use python libraries openCV and tesseract.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/extract-text-from-image-with-python-opencv\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-28T14:24:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-06-03T09:25:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/08\/python-project-detect-extract-text-from-image.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Extract Text from Image with Python &amp; OpenCV - TechVidvan","description":"Learn how to automatically detect and extract text content from image using Python. In this project we will use python libraries openCV and tesseract.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/extract-text-from-image-with-python-opencv\/","og_locale":"en_US","og_type":"article","og_title":"Extract Text from Image with Python &amp; OpenCV - TechVidvan","og_description":"Learn how to automatically detect and extract text content from image using Python. In this project we will use python libraries openCV and tesseract.","og_url":"https:\/\/techvidvan.com\/tutorials\/extract-text-from-image-with-python-opencv\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-08-28T14:24:44+00:00","article_modified_time":"2026-06-03T09:25:20+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/08\/python-project-detect-extract-text-from-image.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/extract-text-from-image-with-python-opencv\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/extract-text-from-image-with-python-opencv\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Extract Text from Image with Python &amp; OpenCV","datePublished":"2020-08-28T14:24:44+00:00","dateModified":"2026-06-03T09:25:20+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/extract-text-from-image-with-python-opencv\/"},"wordCount":471,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/extract-text-from-image-with-python-opencv\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/08\/python-project-detect-extract-text-from-image.jpg","keywords":["extract text from image","machine learning project","Python project"],"articleSection":["Machine Learning Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/extract-text-from-image-with-python-opencv\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/extract-text-from-image-with-python-opencv\/","url":"https:\/\/techvidvan.com\/tutorials\/extract-text-from-image-with-python-opencv\/","name":"Extract Text from Image with Python &amp; OpenCV - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/extract-text-from-image-with-python-opencv\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/extract-text-from-image-with-python-opencv\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/08\/python-project-detect-extract-text-from-image.jpg","datePublished":"2020-08-28T14:24:44+00:00","dateModified":"2026-06-03T09:25:20+00:00","description":"Learn how to automatically detect and extract text content from image using Python. In this project we will use python libraries openCV and tesseract.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/extract-text-from-image-with-python-opencv\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/extract-text-from-image-with-python-opencv\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/extract-text-from-image-with-python-opencv\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/08\/python-project-detect-extract-text-from-image.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/08\/python-project-detect-extract-text-from-image.jpg","width":1200,"height":628,"caption":"python project detect extract text from image"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/extract-text-from-image-with-python-opencv\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Extract Text from Image with Python &amp; OpenCV"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/79752","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=79752"}],"version-history":[{"count":2,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/79752\/revisions"}],"predecessor-version":[{"id":448001,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/79752\/revisions\/448001"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/79754"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=79752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=79752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=79752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}