ImportError: cannot import name mkdtemp:
pip install -U numpy
WindowsError: [Error 2] :
调用tesseract.exe失败,将脚本放在tesseract.exe的文件夹
说明:这个tesseract.exe就相当于在Linux下的 tesseract-orc
IOError: [Errno 2] No such file or directory: 'tesseract.log'
缺少tessdata文件夹
另外的修改方法:修改tesseract_cmd的值,在tesseract.py里面
# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
# tesseract_cmd = 'tesseract'
tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract.exe'
源文档 <https://note.youdao.com/oldweb/>
pytesser
1、PIL、pytesser、tesseract
(1)安装PIL:下载地址:http://www.pythonware.com/products/pil/(CSDN下载)
下载后是一个exe,直接双击安装,它会自动安装到C:\Python27\Lib\site-packages中去,
(2)pytesser:下载地址:http://code.google.com/p/pytesser/,(CSDN下载)
下载解压后直接放C:\Python27\Lib\site-packages(根据你安装的Python路径而不同),同时,新建一个pytesser.pth,内容就写pytesser,注意这里的内容一定要和pytesser这个文件夹同名,意思就是pytesser文件夹,pytesser.pth,及内容都要一样!
(3)Tesseract OCR engine下载:http://code.google.com/p/tesseract-ocr/(CSDN下载)
下载后解压,tessdata文件夹,用其替换掉pytesser解压后的tessdata文件夹即可。(就上面的pytesser文件夹)
1、读取图片
2、图片降噪
3、图片切割
4、图像文本输出
1、获取字符矩阵
2、矩阵进入分类算法
3、输出结果
# 验证码识别,此程序只能识别数据验证码
Pillow
#!/usr/bin/Python
# coding=utf-8
import pytesseract,subprocess,os,tesseract
from nt import chdir
from PIL import Image,ImageOps,ImageFilter,PSDraw
imageObject=Image.open('/Users/intern/Desktop/fdsklajfklas/test.png') #打开一个文件
# print (imageObject)
# print (pytesseract.image_to_string(imageObject))
# print (pytesseract.image_to_string(Image.open('test.JPG')))
# print (open('test.JPG').read())
#
# def cleanImage(imagePath):
# image=Image.open(imagePath)
# image=image.point(lambda x: 0 if x<50 else 255)
# borderImage=ImageOps.expand(image,border=20,fill=255)
# borderImage.save(imagePath)
# path='C:\\Users\\intern\\Desktop\\fdsklajfklas\\test.JPG'
# cleanImage(path)
# p=subprocess.Popen(["tesseract",path,"captcha"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
# p.wait()
# f=open("captcha.txt","rb")
# b=f.read()
# s=b.decode()
# print(s.replace('\n','').replace(' ',''))
# 图片转换为jpg格式
for infile in sys.argv[1:]:
f,e = os.path.splitext(infile)
outfile = f+".jpg"
if infile != outfile:
try:
Image.open(infile).save(outfile)
except IOError:
print ("不能转换",infile)
#创建缩略图
size =(128,128)
for infile in sys.argv[1:]:
outfile = os.path.splitext(infile)[0]+".thumbnail"
if infile != outfile:
try:
im = Image.open(infile)
im.thumbnail(size)
im.save(outfile,"JPEG")
except IOError:
print ("不能创建缩略图",infile)
#确定图片属性和大小
for infile in sys.argv[1:]:
try:
with Image.open(infile) as im:
print(ingile,im.format,"%dX%d"%im.size,im.mode)
except IOError:
pass
im.show() #使用电脑端的软件打开图片
im.crop() #提取子矩形
im.copy() #复制图像
region = region.transpose(Image.ROTATE_180) #粘贴回原图,子图的region必须和给定的box的region吻合。mode不匹配pillow会自动处理
im.paste(region,box)
image = image.copy()
xsize,ysize = image.size()
delta = delta%xsize
part1 = image.crop((0,0,delta,ysize))
image.paste(part1,(xsize_delta,0,xsize,ysize))
r,g,b = im.split() #分离通道
im = Image.merge("RGB",(b,g,r)) #合并通道
im.resize() #集合变换
im.rotate() #集合变换
im.transpose() #集合变换
out = im.resize((128,128))
out = im.rotate(45) #顺时针角度表示
out = im.transpose(Image.FLIP_LEFT_RIGHT) #置换图像
OUT = im.transpose(Image.ROTATE_180) #置换图像
convert() #模式转换
im=Image.open("lena.ppm").convert("L") #模式转换
out=im.filter(ImageFilter.DETAIL) #应用ImageFilter
# multiply each pixel by 1.2 #像素点处理
out=im.point(lambda i:i*1.2) #通过此方法可以简单进行图像处理,通过point和paste可以对图像的某一片区域进行处理
# 处理单独通道
source = im.split()
R,G,B=0,1,2
mask=source[R].point(lambda i:i<100 and 255) #创建mask语句,也可以用下一句表示,当为0的时候返回当前值,255为使用paste的值,中间则用tranceparency效果
mask=source[R].point(lambda i:i out=source[G].point(lambda i:i*0.7) source[G].paste(out,None,mask) im = Image.merge(im.mode,source) enh = ImageEnhance.Contrast(im) #高级图片增强 enh.enhance(1.3).show("30% more contrast") # 可以调整对比度,亮度,色平衡和锐利度 # 动态图,pillow支持FLI/FLC,GIF和其他一些处于实验阶段的格式,TIFF文件同样可以包含数帧图像 im=Image.open("animation.gif") #改程序只支持到下一帧,为了倒回之前的必须重新打开文件 im.seek(1) try: while 1: im.seek(im.tell()+1) except EOFError: #当读取到最后一帧的时候抛出异常 pass # 动态图迭代器 class ImageSequence(): def __int__(self,im): self.im=im def __getitem__(self, ix): try: if ix: self.im.seek(ix) return self.im except EOFError: raise IndexError for frame in ImageSequence(im): print "do something to frame" # Postscript Printing,在图片上添加images,text,graphics im = Image.open("lena.ppm") title = "lena" box = (1*72,2*72,7*72,10*72) ps = PSDraw.PSDraw() ps.begin_document(title) ps.image(box,im,75) ps.rectangle(box) ps.setfont(("HelveticaNarrow-Bold",36) ,w,h,b=ps.textsize(title)) ps.end_document() # mode图像的模式,L为灰度,RGB为真色彩,CMYK为pre-press图像 #其他读取图片的方法 import StringIO im = Image.open(StringIO.StringIO(buffer)) #从string中读取 #从tar中读取 from PIL import TarIO fp = TarIO.TarIO("Imaging.tar","Imaging/test/lena.ppm") im = Image.open(fp) # draft 模式 im= Image.open(file) print("original=",im.mode,im.size) im.draft("L",(100,100)) print("draft=",im.mode,im.size) #定位图片坐标 img = driver.find_element_by_path("").location size=driver.find_element_by_path("").location rangle=(int(location['x'],int(location['y'],int(location['x']+size['width']),int(location['y']+size['height']) i=Image.open('**.png') IMG=i.crop(rangle) IMG.save('E:\\333.png) import tesseract api = tesseract.TessBaseAPI() api.Init(".","eng",tesseract.OEM_DEFAULT) api.SetVariable("tessedit_char_whitelist","0123456789abcdefghijklmnopqrstuvwxyz") api.SetPageSegMode(tesseract.PSM_AUTO) mImgFile ="eurotext.jpg" mBuffer=open(mImgFile,"rb").read() result =tesseract.ProcessPagesBuffer(mBuffer,len(mBuffer),api) print"result(ProcessPagesBuffer)=",result示例2: import cv2.cv as cv import tesseract api = tesseract.TessBaseAPI() api.Init(".","eng",tesseract.OEM_DEFAULT) api.SetPageSegMode(tesseract.PSM_AUTO) image=cv.LoadImage("eurotext.jpg",cv.CV_LOAD_IMAGE_GRAYSCALE) tesseract.SetCvImage(image,api) text=api.GetUTF8Text() conf=api.MeanTextConf() image = Image.open(imagePath) # 打开图片 image = image.point(lambdax: 0 if x < 2 else 255) # 处理图片上的每个像素点,使图片上每个点“非黑即白” borderImage = ImageOps.expand(image, border=20, fill='white') borderImage.save(imagePath) captchaResponse = f.read().replace(" ", "").replace("\n", "")# 清除读取的空格和换行 driver.refresh() #刷新 image = image.convert('L') #转化到灰度图 # coding=utf-8 import tesseract,sys,os,pytesseract,re from time import sleep from PIL import Image,ImageOps,ImageFilter,PSDraw from PIL import ImageEnhance from selenium import webdriver from pytesseract import * reload(sys) sys.setdefaultencoding("utf-8") driver = webdriver.Chrome() driver.implicitly_wait(30) driver.get("http://10.84.62.91/intern/index.php") # driver.implicitly_wait(30) #driver.find_element_by_xpath("/html/body/div/table/tbody/tr/td/form/table/tbody/tr[2]/td[2]/input") # driver.implicitly_wait(30) #driver.find_element_by_xpath("/html/body/div/table/tbody/tr/td/form/table/tbody/tr[3]/td[2]/input") # driver.implicitly_wait(30) a = driver.find_element_by_id("imgcode").get_attribute("src") driver.get(a) print a driver.get_screenshot_as_file("pic.png") im = Image.open("pic.png") im = im.crop((0,0,77,21)) # im = im.point(lambdax:0 if x<2 else 255) #图片降噪截取矩形,非黑即白 enhancer = ImageEnhance.Contrast(im) enhancer = enhancer.enhance(4) im = im.convert('L') #转化成灰度图 borderImage = ImageOps.expand(im,border=20,fill="white") borderImage.save("pic.png") tex=image_to_string(im).replace(" ",'').replace('\n','').upper() print tex # driver.back()