驗證碼分割-完全分開那種,不解釋
阿新 • • 發佈:2019-01-05
def pic_location(start_width,all_width,height,pixel):
x0_list=[]
y0_list=[]
x1_list=[]
y1_list=[]
for w in range(start_width,all_width):
for h in range(height):
if pixel[w,h]==0:
x0_list.append(w)
break
x0=x0_list[0] #x0座標得出
black=0
for w in range(x0+2,all_width):
for h in range(height):
if pixel[w,h]==0:
black=black+1
break
if black==0:
x1=w #獲得x1座標
break
black=0
for h in range(height):
for w in range(x0,x1+1):
if pixel[w,h]==0:
y0_list.append(h)
y0=y0_list[0 ]
for h in range(height-1,-1,-1):
for w in range(x0,x1+1):
if pixel[w,h]==0:
y1_list.append(h)
y1=y1_list[0] #獲得y1座標
box=(x0,y0,x1,y1) #獲得單個字元切割點
start_width=x1
return box,start_width
num=0
for j in range(99):
start_width=0
for i in range (5):
try:
img=Image.open('c:/python/clear_pic/'+str(j)+'.jpg')
pixel=img.load()
all_width=img.size[0]
height=img.size[1]
box,start_width=pic_location(start_width,all_width,height,pixel)
child_pic=img.crop(box)
child_pic.save('c:/python/cut_pic/'+'cut_pic_'+str(num)+'.jpg')
num=num+1
except Exception as e:
pass