#coding:utf-8 import cv2 import numpy as np bet = 64 padding = 2 x = 50 y = 90 width = 320 height = 36 images = [] class ImageSplitter: def __init__(self,path): self.path = path self.src = cv2.imread(path) self.src = cv2.resize(self.src,(420,594),cv2.INTER_CUBIC) self.src = cv2.cvtColor(self.src,cv2.COLOR_BGR2GRAY) def split(self): for i in range(0,6): _y = y + bet*i dst = self.src[_y:_y+height,x:x+width] dst = cv2.bitwise_not(dst) ret,dst = cv2.threshold(dst,127,255,0) dst = self.removeMargin(dst) dst = self.addMargin(dst) images.append(dst) return images def removeMargin(self,im): image = im left = [width,width,width] for j in range(1,3): for i in range(1,width): if(image[(int)(height*j/4),i]>=127): left[j-1] = i break right = [0,0,0] for j in range(1,3): for i in list(reversed(range(width))): if(image[(int)(height*j/4),i]>=127): right[j-1] = i break l = min(left)-10 r = max(right)+10 w = r-l image = image[0:height,l:r] top = [height] bottom = [0] for j in range((int)(w/2)): for i in range(1,height): if image[i,j*2]>=127: top.append(i) break for i in list(reversed(range(height))): if image[i,j*2]>=127: bottom.append(i) break t = min(top)-2 b = max(bottom)+2 image = image[t:b,0:w] return image def addMargin(self,im): _x = im.shape[1]+10 _y = im.shape[0]+10 _back = np.zeros((_y,_x),np.uint8) _back[5:_y-5,5:_x-5] = im _image = _back return _image