The following are 30code examples of pytesseract.image_to_string().You can vote up the ones you like or vote down the ones you don't like,and go to the original project or source file by following the links above each example.You may also want to check out all available functions/classes of the modulepytesseract, or try the search function.
Example #1
def get_battle_id(img_path: str): img = Image.open(img_path) region = img.crop((1286, 15, 1378, 62)) THRESHOLD = 200 BINARY_TABLE = [0 if i < THRESHOLD else 1 for i in range(256)] text = image_to_string( region.convert('L').point(BINARY_TABLE, '1'), config='--psm 7 --oem 3 -c tessedit_char_whitelist=/1234') print(text) try: x = int(text[0]) except IndexError: print("Failed to recognize battle id.") return 0 except ValueError: print("Failed to recognize battle id.") return 0 else: return x
Example #2
def deobfuscator(dict_of_dicts): #====Work backwards==== #Build graph from dict_of_dicts: graph_from_dict = nx.DiGraph(dict_of_dicts) #Get adjacency matrix of graph graph_array = nx.to_numpy_array(graph_from_dict) #Change 1's to 255's to save as an image graph_array[graph_array == 1] = 255 image_from_array = Image.fromarray(graph_array).convert("L") #We can send the array directly to OCR, but I like to see the image. image_from_array.save("obfuscated.png") #Run OCR on our image return pytesseract.image_to_string("obfuscated.png")
Example #3
def getImgFromScreenCapture(ques, ans_one, ans_two, ans_thr): question = os.system("screencapture -R {} ./question_screenshot.png".format(ques)) answer_one = os.system("screencapture -R {} ./answers_one.png".format(ans_one)) answer_two = os.system("screencapture -R {} ./answers_two.png".format(ans_two)) answer_thr = os.system("screencapture -R {} ./answers_thr.png".format(ans_thr)) question_img = Image.open("./question_screenshot.png") answer_one_img = Image.open("./answers_one.png") answer_two_img = Image.open("./answers_two.png") answer_thr_img = Image.open("./answers_thr.png") question_enh = getImageFromImageEnhanceForQuestion(question_img) ans_one_enh = getImageFromImageEnhance(answer_one_img) ans_two_enh = getImageFromImageEnhance(answer_two_img) ans_thr_enh = getImageFromImageEnhance(answer_thr_img) #使用简体中文解析图片 print('OCR ' + datetime.datetime.now().strftime('%H:%M:%S')) question_text = pytesseract.image_to_string(question_enh, lang='chi_sim') question = question_text answers = ['','',''] return question, answers
Example #4
def ocr_get_port(self, data): """ 用ocr提取图片中的端口 :param data: 返回的图片二进制流结果 :return: """ f = open('port.png', 'wb') f.write(data) f.close() pytesseract.pytesseract.tesseract_cmd = 'C://Program Files//Tesseract-OCR//tesseract.exe' port = pytesseract.image_to_string(Image.open('port.png'), config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789') # 删除图片 os.remove('port.png') return port
Example #5
def autocaptcha(path): """Auto identify captcha in path. Use pytesseract to identify captcha. Args: path: string, image path. Returns: string, OCR identified code. """ im = Image.open(path) im = im.convert('L') im = ImageEnhance.Contrast(im) im = im.enhance(3) img2 = Image.new('RGB', (150, 60), (255, 255, 255)) img2.paste(im.copy(), (25, 10)) # TODO: add auto environment detect return pytesseract.image_to_string(img2)
Example #6
def create_session(self): """ create a session by solving captcha challenge """ self.session['timestamp'] = int(time.time() * 1000) url = "http://www.indianrail.gov.in/enquiry/captchaDraw.png?{}".format(self.session['timestamp']) r = requests.get(url) self.session['cookies'] = r.cookies try: f = BytesIO(r.content) except OSError: return None im = Image.open(f) text = pytesseract.image_to_string(im, lang = 'eng') try: self.session['captcha'] = eval(text.split("=")[0]) except: self.create_session()
Example #7
def handle_tweet(self, tweet_json):screen_name = tweet_json["user"]["screen_name"]id = tweet_json["id_str"]text = tweet_json["text"].replace("\\", "")# Get media if presenttry:urls = [x["media_url"].replace("\\", "") for x in tweet_json["entities"]["media"] if x["type"] == "photo"]for url in urls:response = requests.get(url)img = Image.open(io.BytesIO(response.content))# Extract text from imageimg_text = pytesseract.image_to_string(img)text += f' . {img_text}'except KeyError:passlink = f'https://twitter.com/{screen_name}/status/{id}'try:self.tweet_callback(text, screen_name, link)except:pass
Example #8
def full_OCR(self): bounded = self.img.copy() res = np.zeros_like(self.gray_img) string = image_to_string(Image.open(self.image_file)) if string == u'': return bounded, res boxes = image_to_boxes(Image.open(self.image_file)) boxes = [map(int, i) for i in [b.split(" ")[1:-1] for b in boxes.split("\n")]] for box in boxes: b = (int(box[0]), int(self.h - box[1]), int(box[2]), int(self.h - box[3])) cv2.rectangle(bounded, (b[0], b[1]), (b[2], b[3]), (0, 255, 0), 2) cv2.rectangle(res, (b[0], b[1]), (b[2], b[3]), 255, -1) return bounded, res
Example #9
def extracttext(imgpath, preprocess): if imgpath.startswith('http://') or imgpath.startswith('https://') or imgpath.startswith('ftp://'): image = url_to_image(imgpath) else: image = cv2.imread(imgpath) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if preprocess == "thresh": gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] elif preprocess == "blur": gray = cv2.medianBlur(gray, 3) filename = "{}.png".format(os.getpid()) cv2.imwrite(filename, gray) text = pytesseract.image_to_string(Image.open(filename)) os.remove(filename) return {"text": text}
Example #10
def vcode(self): r = self._session.get( 'https://jy.yongjinbao.com.cn/winner_gj/gjzq/user/extraCode.jsp', params={'randomStamp': random.random()} ) r.raise_for_status() # 通过内存保存数据 img_buffer = BytesIO(r.content) img = Image.open(img_buffer) code = pytesseract.image_to_string(img) img.close() img_buffer.close() if self.code_rule.findall(code) == []: raise VerifyCodeError('Wrong verify code: %s' % code) else: logger.debug('Verify Code is: %s' % code) return code
Example #11
def detect_gf_result(image_path): from PIL import ImageFilter, Image import pytesseract img = Image.open(image_path) for x in range(img.width): for y in range(img.height): if img.getpixel((x, y)) < (100, 100, 100): img.putpixel((x, y), (256, 256, 256)) gray = img.convert('L') two = gray.point(lambda x: 0 if 68 < x < 90 else 256) min_res = two.filter(ImageFilter.MinFilter) med_res = min_res.filter(ImageFilter.MedianFilter) for _ in range(2): med_res = med_res.filter(ImageFilter.MedianFilter) res = pytesseract.image_to_string(med_res, config='-psm 6') return res.replace(' ', '')
Example #12
def get_name(img): # cv2.imshow("method3", img) # cv2.waitKey() print('name') _, _, red = cv2.split(img) #split 会自动将UMat转换回Mat red = cv2.UMat(red) red = hist_equal(red) red = cv2.adaptiveThreshold(red, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 151, 50) # red = cv2.medianBlur(red, 3) red = img_resize(red, 150) img = img_resize(img, 150) # showimg(red) # cv2.imwrite('name.png', red) # img2 = Image.open('address.png') # img = Image.fromarray(cv2.UMat.get(red).astype('uint8')) #return get_result_vary_length(red, 'chi_sim', img, '-psm 7') return get_result_vary_length(red, 'chi_sim', img, '--psm 7') # return punc_filter(pytesseract.image_to_string(img, lang='chi_sim', config='-psm 13').replace(" ",""))
Example #13
def captcha_recognize(img_path): import pytesseract im = Image.open(img_path).convert("L") # 1. threshold the image threshold = 200 table = [] for i in range(256): if i < threshold: table.append(0) else: table.append(1) out = im.point(table, "1") # 2. recognize with tesseract num = pytesseract.image_to_string(out) return num
Example #14
def ocr_image(orig_image_arr): otsu_thresh_image = PIL.Image.fromarray(process_image(orig_image_arr)) return image_to_string(otsu_thresh_image, lang="letsgodigital", config="-psm 100 -c tessedit_char_whitelist=.0123456789")
Example #15
def image_ocr(image_path): """ 识别图像中的英文 :return: """ # 英文:lang='eng' # 中文:lang='chi_sim' return pytesseract.image_to_string(Image.open(image_path), lang='eng')
Example #16
def jpg_to_txt(tesseractLoc, filename): # This is added so that python knows where the location of tesseract-OCR is pytesseract.pytesseract.tesseract_cmd = tesseractLoc # again using the function return value sourceImg = get_path_of_source(filename).with_suffix('.jpg') # Using pillow to open image img = Image.open(sourceImg) filenameOfImg = img.filename text = pytesseract.image_to_string(img) #calling the function which was defined above this function save_to_file_as_txt(filenameOfImg, text)
Example #17
def get_turns(): img = Image.open(screenshot_path) img_size = img.size gap = (img_size[0] - 1920) / 2 left = gap + 1280 right = left + 100 bottom = img_size[1] / 20 region = img.crop((left, 0, right, bottom)) text = image_to_string(region, config='--psm 7 -c tessedit_char_whitelist=/1234') print(text) return text
Example #18
def ocr(imgfile): print "[+] Running OCR on:", imgfile text = pytesseract.image_to_string(Image.open(imgpath + imgfile)) if debug: print "----------------------------" print "OCR Message:", text print "----------------------------" if text.find("YUKARIDAK") >= 0 or text.find("SANSLI KISI") >= 0 or text.find("SANSLI KiSi") >= 0 or text.find("KATILIM YAPAN") >= 0: # print "[!] Phishing tweet detected!\n"return 1 else:# print imgpath + imgfileos.remove(imgpath + imgfile)return 0
Example #19
def get_captcha(self): url = 'http://www.96369.net/Other/ValidateCode.aspx' response = self.do_request(url, method='GET', type='binary') img = Image.open(io.BytesIO(response)) img = img.convert('L') # 二值化 threshold = 160 table = [] for i in range(256): if i < threshold: table.append(0) else: table.append(1) # 由于都是数字 # 对于识别成字母的 采用该表进行修正 rep = {'O': '0', 'I': '1', 'L': '1', 'Z': '2', 'S': '8' } img = img.point(table, '1') img = img.convert('RGBA') rsp = pytesseract.image_to_string(img, config='-psm 6') try: answer = eval(rsp) return answer, self.get_cookies() except: return None, None
Example #20
def test02(): from PIL import Image import pytesseract """谷歌图片识别的包:tesseract""" # 使用pil加载一张图片到内存中,返回图片对象 img = Image.open('test.jpg') # 调用tesseract进行识别,返回一个data data = pytesseract.image_to_string(img) # 输出结果 print(data)
Example #21
def detect_yh_client_result(image_path): """封装了tesseract的识别,部署在阿里云上,服务端源码地址为: https://github.com/shidenggui/yh_verify_code_docker""" image = Image.open(image_path) code = pytesseract.image_to_string(image, config='-psm 7') return code.replace(' ', '')
Example #22
def test_tesseract(self): # Open pdf with Wand with wandimage(filename='/input/tests/data/test.pdf') as wand_image: img_buffer = np.asarray(bytearray(wand_image.make_blob(format='png')), dtype='uint8') bytesio = io.BytesIO(img_buffer) test_string = pytesseract.image_to_string(Image.open(bytesio)) self.assertTrue(type(test_string) == str)
Example #23
def image_ocr(self, path): import pytesseract from PIL import Image try: img = Image.open(path) text = pytesseract.image_to_string(img) except: text = '' return text
Example #24
def show_ocr_result(frame): start_time = time.time() text = pytesseract.image_to_string(frame, lang="eng", config="--psm 8") print(text) util.display_total_time(start_time) start_time = time.time() pytess_result = pytesseract.image_to_boxes(frame, lang="eng", config="--psm 8", output_type=pytesseract.Output.DICT) print(pytess_result) util.display_total_time(start_time) bbox_list = list() for i, _ in enumerate(pytess_result['bottom']): tl = (pytess_result['left'][i], pytess_result['bottom'][i]) br = (pytess_result['right'][i], pytess_result['top'][i]) bbox_list.append((tl, br)) util.show_frame(frame, bbox_list=bbox_list, wait_flag=True) start_time = time.time() pytess_data = pytesseract.image_to_data(frame, lang="eng", config="--psm 8", output_type=pytesseract.Output.DICT) print(pytess_data) util.display_total_time(start_time) bbox_list = list() for i, conf in enumerate(pytess_data['conf']): if int(conf) != -1: print("\tconf: {}".format(conf)) tl = (pytess_data['left'][i], pytess_data['top'][i]) br = (tl[0]+pytess_data['width'][i], tl[1]+pytess_data['height'][i]) bbox_list.append((tl, br)) util.show_frame(frame, bbox_list=bbox_list, wait_flag=True)
Example #25
def standard_test(self): fnum = self.start_fnum time_queue = list() disp_dict = dict() while fnum < self.stop_fnum: start_time = time.time() fnum += self.step_size frame = util.get_frame(self.capture, fnum, gray_flag=True) frame = frame[300:340, 80:220] # 300:340, 200:320 frame = self.param_filter(frame) if self.contour_flag: frame = self.contour_filter(frame) if self.ocr_flag: conf_text = "--psm 7" # Single test line mode. if self.ocr_mode_flag: # Single word mode. conf_text = "--psm 8" text = pytesseract.image_to_string(cv2.bitwise_not(frame), lang="eng", config=conf_text) disp_dict["OCR"] = text util.display_pa_fps(start_time, time_queue, disp_dict) cv2.imshow(self.window_name, frame) if cv2.waitKey(self.step_delay) & 0xFF == ord('q'): break # Apply filters to frame according to GUI parameters.
Example #26
def image_to_text(img, only_digits=False): text = pytesseract.image_to_string(img) text = re.sub('[\W]', '', text) text = text.strip() if only_digits: text = text.upper() for r in replace_table: text = text.replace(r, replace_table[r]) return text
Example #27
def solve_captcha(self, url, session): """Solve a Bluecoat CAPTCHA for the provided session.""" # Downloads CAPTCHA image and saves to current directory for OCR with tesseract # Returns CAPTCHA string or False if error occurred jpeg = 'captcha.jpg' headers = {'User-Agent': self.useragent} try: response = session.get(url=url, headers=headers, verify=False, stream=True) if response.status_code == 200: with open(jpeg, 'wb') as f: response.raw.decode_content = True shutil.copyfileobj(response.raw, f) else: click.secho('[!] Failed to download the Bluecoat CAPTCHA.', fg='red') return False # Perform basic OCR without additional image enhancement text = pytesseract.image_to_string(Image.open(jpeg)) text = text.replace(" ', '").replace("[', 'l").replace("'', '") # Remove CAPTCHA file try: os.remove(jpeg) except OSError: pass return text except Exception as error: click.secho('[!] Error processing the Bluecoat CAPTCHA.'.format(error), fg='red') return False
Example #28
def vcode(self): # 获取校验码 r = self._session.get('https://trade.gf.com.cn/yzm.jpgx') r.raise_for_status() # 通过内存保存图片,进行识别 img_buffer = BytesIO(r.content) img = Image.open(img_buffer) if hasattr(img, "width"): width, height = img.width, img.height else: width, height = img.size for x in range(width): for y in range(height): if img.getpixel((x, y)) < (100, 100, 100): img.putpixel((x, y), (256, 256, 256)) gray = img.convert('L') two = gray.point(lambda x: 0 if 68 < x < 90 else 256) min_res = two.filter(ImageFilter.MinFilter) med_res = min_res.filter(ImageFilter.MedianFilter) for _ in range(1): med_res = med_res.filter(ImageFilter.MedianFilter) # 通过tesseract-ocr的工具进行校验码识别 vcode = pytesseract.image_to_string(med_res) img.close() img_buffer.close() vcode = vcode.replace(' ', '') if self.code_rule.findall(vcode) != []: logger.debug('vcode is: %s' % vcode) return vcode else: raise VerifyCodeError('verify code error: %s' % vcode)
Example #29
def extract(model_name): print ("extract") if request.method == 'GET': return jsonify(error='Use POST method to send image.'), 400 #total_predictions = request.args.get('total') # Wait for the model to finish loading. image = request.files.get('image') thres = request.values.get("th") img = Image.open(image.stream).convert('RGB') #vis_objects(np.array(image_array), objects).save("c:\\temp\\data.png") s = "" for obj in ouputObjects: if (obj['prob'] > (int(thres)/100)): coordinates = obj['bbox'] print (coordinates) cropped = img.crop( ( coordinates[0], coordinates[1], coordinates[2], coordinates[3] ) ) file = "c:\\temp\\" +str(obj['prob']) +".jpg" cropped.save(file) print (file) print (image_to_string(Image.open(file))) s += image_to_string(Image.open(file)) print (s) return s
Example #30
def ocr_question_extract(im): # git@github.com:madmaze/pytesseract.git global pytesseract try: import pytesseract except: print "[ERROR] pytesseract not installed" return im = im.crop((127, 3, 260, 22)) im = pre_ocr_processing(im) # im.show() return pytesseract.image_to_string(im, lang='chi_sim').strip()
FAQs
How do I create an OCR in Python? ›
Building an Optical Character Recognition in Python
We first need to make a class using “pytesseract”. This class will enable us to import images and scan them. In the process it will output files with the extension “ocr.py”. Let us see the below code.
Python-tesseract is an optical character recognition (OCR) tool for python. That is, it will recognize and “read” the text embedded in images. Python-tesseract is a wrapper for Google's Tesseract-OCR Engine.
How does Pytesseract work? ›Python-tesseract is an optical character recognition (OCR) tool for python. That is, it will recognize and “read” the text embedded in images. Python-tesseract is a wrapper for Google's Tesseract-OCR Engine .
What is difference between Tesseract and Pytesseract? ›Both are OCR wrappers for Python; however, pytesseract is based on Googles OCR API and tesseract isn't. I would suggest using pytesseract based on the fact that it will be maintained better, but with that being said, try them both out and use whichever works better for you.
How do I use Tesseract to read text from an image? ›Create a Python tesseract script
Create a project folder and add a new main.py file inside that folder. Once the application gives access to PDF files, its content will be extracted in the form of images. These images will then be processed to extract the text.
Tesseract is an open source text recognition (OCR) Engine, available under the Apache 2.0 license. It can be used directly, or (for programmers) using an API to extract printed text from images. It supports a wide variety of languages.
How do you OCR an image in Python? ›Extract Text From Images in Python (OCR) - YouTube
Is OpenCV an OCR? ›OpenCV package is used to read an image and perform certain image processing techniques. Python-tesseract is a wrapper for Google's Tesseract-OCR Engine which is used to recognize text from images. Download the tesseract executable file from this link.
Can Tesseract read handwriting? ›In the current work, Tesseract 2.01 is used for developing user-specific handwriting recognition models, viz., the language sets, for the iJIT system. To generate the language sets for each user, Tesseract is trained with labeled handwritten data samples of isolated and free-flow texts of lower case Roman script.
Is Tesseract free? ›Tesseract is an optical character recognition engine for various operating systems. It is free software, released under the Apache License.
Can Tesseract read PDF? ›
Tesseract is an excellent open-source engine for OCR. But it can't read PDFs on its own.
Does Tesseract need Internet? ›Tesseract OCR is an offline tool, which provides some options it can be run with.
Does Pytesseract need Tesseract? ›You would be needing to install tesseract. Check out the above documentation on the installation.
How do you improve Tesseract accuracy? ›It is also possible to add words to the word list Tesseract uses to help recognition, or to add common character patterns, which can further help to improve accuracy if you have a good idea of the sort of input you expect. This is explained in more detail in the Tesseract manual.
What is better than Tesseract OCR? ›Google does well on the scanned email and recognizes the text in the smartphone-captured document similarly well as ABBYY. However it is much better than Tesseract or ABBYY in recognizing handwriting, as the second result image shows: still far from perfect, but at least it got some things right.
How do I convert an image to text in Python? ›- from os import closerange.
- from PIL import Image.
- import pytesseract as tess.
- tess. pytesseract. tessetact_cmd = r'give your PATH TO TESSETACT.EXE'
- image = r'complete path to image file'
- text = tess. image_to_string(Image. open(image), lang="eng")
- print(text)
Tesseract. Tesseract is the most acclaimed open-source OCR engine of all and was initially developed by Hewlett-Packard. It's a free software under Apache license that's sponsored by Google since 2006. Tesseract OCR engine is considered one of the most accurate, freely available open-source systems available.
How can I extract text from an image? ›Extract Text From an Image: Copy Text From Images & PDFs - YouTube
How do I convert scanned files to searchable PDF using Python and Pytesseract? ›Convert a Scanned PDF into a Searchable PDF in Python - YouTube
How do I read text in OpenCV? ›- Reading a sample Image. import cv2. Read the image using cv2. imread() method and store it in a variable “img”. ...
- Converting Image to String. import pytesseract. Set the tesseract path in the code pytesseract.pytesseract.tesseract_cmd=r'C:Program FilesTesseract-OCRtesseract.exe'
Can I train Tesseract? ›
Luckily, you can train your Tesseract so it can read your font easily.
How accurate is Tesseract OCR? ›Combinations of the first three preprocessing actions are said to boost the accuracy of Tesseract 4.0 from 70.2% to 92.9%.
How do you get Pytesseract? ›Point pytesseract at your tesseract installation
Create a Python script (a . py-file), or start up a Jupyter notebook. At the top of the file, import pytesseract , then point pytesseract at the tesseract installation you discovered in the previous step.
We want to use Tesseract from our windows command line and to do that, we have to add Tesseract to our path in the system's environment variable. To do so, click on your start button on windows and search “environment variable”. You will see a result called “Edit the system environment variables”. Click on that.
How do I convert an image to text in Python? ›- from os import closerange.
- from PIL import Image.
- import pytesseract as tess.
- tess. pytesseract. tessetact_cmd = r'give your PATH TO TESSETACT.EXE'
- image = r'complete path to image file'
- text = tess. image_to_string(Image. open(image), lang="eng")
- print(text)
You would be needing to install tesseract. Check out the above documentation on the installation.
Does Tesseract need Internet? ›Tesseract OCR is an offline tool, which provides some options it can be run with.
How do I find Tesseract location? ›- #1. Install tesseract using windows installer available at: https://github.com/UB-Mannheim/tesseract/wiki.
-
- #2. Note the tesseract path from the installation. ...
-
- #3. pip install pytesseract.
-
- #4. Set the tesseract path in the script before calling image_to_string:
-
You can capture text from a scanned image, upload your image file from your computer, or take a screenshot on your desktop. Then simply right click on the image, and select Grab Text. The text from your scanned PDF can then be copied and pasted into other programs and applications.
How can I extract text from an image? ›Extract Text From an Image: Copy Text From Images & PDFs - YouTube
Is Tesseract free? ›
Tesseract is an optical character recognition engine for various operating systems. It is free software, released under the Apache License.
How do I know if Tesseract is installed? ›To verify if Tesseract is successfully installed, you can hit your terminal and type the following. If you receive a few lines of prompt similar to the one below, your Tesseract is installed correctly. Otherwise, you might want to check what has gone wrong by starting from your PATH variable in your system.
How do I set an environment variable in Tesseract? ›Installing Tesseract
Make sure your TESSDATA_PREFIX environment variable is set correctly: Go to Control Panel -> System -> Advanced System Settings -> Advanced tab -> Environment Variables... button. In System variables window scroll down to TESSDATA_PREFIX.
Go to https://smallseotools.com/text-to-image/(if you aren't already there) Write the desired text or paste it from the clipboard in the big text box. Next, you have to choose the desired options such as Text Color, Font Style, Font Size, Background Color, and Text Background Color for the out image.
What is a text image? ›What are images of text? An image of text refers to when readable text is presented inside an image, including text that has been presented in a fixed image form in order to achieve a certain visual style.