

This was made by ( +KevinKwok on Google+), and Guillermo Webster.Įarly in October 2013, coincidentally less than a week before I developed the first prototype of this extension, xkcd published a comic (shown on the right) which somewhat ironically depicts the impetus for the extension. Right-click and you can erase the words from an image, edit the words, or even translate it into a different language. Hit Ctrl+C to copy the text, where you can paste it into a search bar, a Word document, an email or a chat window. You can drag over a few lines and watch as a semitransparent blue box highlights the text, helping you keep track of where you are and what you’re reading. You can watch as moving your cursor over a block of words changes it into the little I-beam. Interaction with this second type of text has always been a second class experience, the only way to search or copy a sentence from an image would be to do as the ancient monks did, manually transcribing regions of interest. Text = pytesseract.Words on the web exist in two forms: there’s the text of articles, emails, tweets, chats and blogs- which can be copied, searched, translated, edited and selected- and then there’s the text which is shackled to images, found in comics, document scans, photographs, posters, charts, diagrams, screenshots and memes. # Applying tesseract OCR on the cropped image # Loop over contours and crop and extract the text file Img_contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL, # Appplying dilation on the threshold imageĭilation = cv2.dilate(threshimg, rect_kernel, iterations = 1) Rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (18, 18)) # Specifying kernel size and structure shape. Ret, threshimg = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV) Gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Next, we have passed the rectangle area onto pytesseract for extracting text from it and then writing it into the text file. Next, we found the contours and looped over them, chopping the rectangle area. In this, we first converted the image to grayscale and then specified the kernel shape and size. Tesseract is ideal for scanning clean documents, and we can easily convert the image's text from OCR to Word or to any other required format. The above dictionary has information about your input image. The above code returns the following output. Text = pytesseract.image_to_string(img, config=config) Let's merge all the above code and execute it. Print(text) Complete Code: Extract text from image Python text = pytesseract.image_to_string(img, config=config)įinally, we can print the extracted text form Image. Next, we convert from image to string using the method image_to_string(). _cmd = 'C:/Program Files/Tesseract-OCR/tesseract.exe' If you have not configured the tesseract executable in your System variables PATH, include the following. Here, we have set the configuration custom options. Next, we used the imread() function to load the test image from the specified location. Suppose we have the following test image located in the same working directory.įirst, we have created a Python file and imported all the necessary modules at the top. Python Code to Extract Text From Image using Tesseract This is extremely useful in an occurrence where a lot of documentation is involved, like educational institutes, news agencies, government workplaces, etc. The best part is that it supports an extensive variety of languages. It is widely used to extract text from images or documents because it provides a more accurate result. It is an open-source text recognition engine. Python-tesseract is an optical character recognition (OCR) tool for Python. It also saves a lot of time by frequently transferring paper documents into electronic files. This process can also help in reducing document file sizes for easier transfer and sharing. The OCR module is designed with more advanced features for performing additional functionalities. It works on optical recognition technology. It is basically used to convert scanned documents into searchable text files. Thus, everything from checking records, bank articulations, receipts, transcribed reports, coupons, hand writing, documents and so forth, all falls under the OCR. OCR has applications in a wide scope of enterprises and capacities. OCR (Optical Character Recognition) is a process of recognising text in an image and converting it into text.
