介绍
怎么在python中利用pytesseract对本地的图片文字进行识别?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
具有方法如下:
# !/usr/bin/env python3 #,- *安康;编码:utf-8 - * - import 水珠 得到os import 路径 import 操作系统 import pytesseract 得到PIL import 形象 得到queue import 队列 import 线程 import datetime import cv2 def convertimg (picfile, outdir): & # 39;才能& # 39;& # 39;调整图片大小,对于过大的图片进行压缩 picfile才能:,,图片路径 outdir才能:,,图片输出路径 & # 39;才能& # 39;& # 39; 时间=img 才能;Image.open (picfile) 宽度,才能,height =img.size while 才能;(width *, height 祝辞,4000000):,#,该数值压缩后的图片大约,两百多k ,,,width =, width //2 ,,,height =, height //2 时间=new_img 才能;img.resize((宽度,高度),Image.BILINEAR) new_img.save才能(path.join (outdir, os.path.basename (picfile))) def baiduOCR (ts_queue): while 才能;not  ts_queue.empty (): ,,,picfile =, ts_queue.get () ,,,filename =, path.basename (picfile) ,,,outfile =, & # 39; D: \ \ pythonProject \ scrapy研究\ IpProxy \ port_zidian.txt& # 39; ,,,img =, cv2.imread (picfile, cv2.IMREAD_COLOR) ,,,print(“正在识别图片:\ t", +,文件名) ,,,message =, pytesseract.image_to_string (img lang =, & # 39; eng # 39;) ,,,message =, message.replace (& # 39; & # 39;,, & # 39; & # 39;) ,,,message =, message.replace (& # 39; \ n # 39;,, & # 39; & # 39;) ,,,#,message =, client.basicAccurate (img),, #,通用文字高精度识别,每天,800,次免费 ,,,#打印(“识别成功!“)) ,,,试一试: ,,,,,filename1 =, filename.split(& # 39; # 39;公司)[0] ,,,,,filename1 =, & # 39; & # 39; . join (filename1) ,,,,,with 开放(输出文件,& # 39;a + & # 39;), as : ,,,,,,,fo.writelines (& # 39; \ & # 39; & # 39;, +, filename1 +, & # 39; \ & # 39; & # 39;, +, & # 39;: & # 39;, +, message +, & # 39;, & # 39;) ,,,,,,,fo.writelines (& # 39; \ n # 39;) ,,,,,,,#,fo.writelines (“+”, *, 60, +, & # 39; \ n # 39;) ,,,,,,,#,fo.writelines(“识别图片:\ t", +, filename +,“\ n", *, 2) ,,,,,,,#,fo.writelines(“文本内容:\ n") ,,,,,,,#,#,输出文本内容 ,,,,,,,#,for text 拷贝message.get (& # 39; words_result& # 39;): ,,,,,,,#,,,fo.writelines (text.get(& # 39;词# 39;),+,& # 39;\ n # 39;) ,,,,,,,#,fo.writelines (& # 39; \ n # 39;, *, 2) ,,,,,os.remove(文件名) ,,,,,印刷(“识别成功!“) ,,,除了: ,,,,,印刷(& # 39;识别失败& # 39;) ,,,print(“文本导出成功!“) ,,,print () def duqu_tupian (dir): ts_queue 才能=,队列(10000) outdir 才能=dir #,才能if path.exists(输出文件): #才能,,,os.remove(输出文件) if 才能;not  path.exists (outdir): ,,,os.mkdir (outdir) 打印才能(“压缩过大的图片…“) #才能,首先对过大的图片进行压缩,以提高识别速度,将压缩的图片保存与临时文件夹中 尝试才能: ,,,for picfile 拷贝glob.glob (r" D: \ \ pythonProject \ scrapy \研究IpProxy \ tmp \ *”): ,,,,,convertimg (picfile, outdir) ,,,print(“图片识别…“) ,,,for picfile 拷贝glob.glob (“tmp1/*“): ,,,,,ts_queue.put (picfile) ,,,,,# baiduOCR (picfile,输出文件) ,,,,,# os.remove (picfile) ,,,print(& # 39;图片文本提取结束!文本输出结果位于文件中# 39;公司,) ,,,# os.removedirs (outdir) ,,,return ts_queue 除了才能: ,,,print(& # 39;失败& # 39;) if __name__ ==,“__main__": 时间=start 才能;datetime.datetime.now () .replace(微秒=0) 时间=t 才能;& # 39;tmp1& # 39; null null null null null null null null null null null null null null怎么在python中利用pytesseract对本地的图片文字进行识别