程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python image vertical cutting image verification code image recognition processing

編輯:Python


 ​

 ​​

這樣的一張圖片,Cut with regular equal width:

def
sliceImg(
img,
outDir,
count
=
4):

w, h = img. size
eachWidth = int( w / count)
for i in range( count):
box = ( i * eachWidth, 0, ( i + 1) * eachWidth, h)
img. crop( box). save( outDir + str( i) + ".png")
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

 

 ​

   ​​​ ​

  ​​​ ​

   ​​​ ​

​​

It can be seen that the cutting effect is poor,3all cut8了.

We can change our minds,Pixel judgment in the vertical direction is performed before and after the target position,Determine that a column has the fewest black pixels,It's the cutting point.代碼改成:

def
smartSliceImg(
img,
outDir,
count
=
4,
p_w
=
3):

'''
:param img:
:param outDir:
:param count: How many pictures are in the picture
:param p_w: Judge how many pixels in the cutting place
:return:
'''
w, h = img. size
pixdata = img. load()
eachWidth = int( w / count)
beforeX = 0
for i in range( count):

allBCount = []
nextXOri = ( i + 1) * eachWidth

for x in range( nextXOri - p_w, nextXOri + p_w):
if x >= w:
x = w - 1
if x < 0:
x = 0
b_count = 0
for y in range( h):
if pixdata[ x, y] == 0:
b_count += 1
allBCount. append({ 'x_pos': x, 'count': b_count})
sort = sorted( allBCount, key = lambda e: e. get( 'count'))

nextX = sort[ 0][ 'x_pos']
box = ( beforeX, 0, nextX, h)
img. crop( box). save( outDir + str( i) + ".png")
beforeX = nextX
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.

 

 ​

   ​​​ ​ ​

   ​​​ ​ ​

    ​​​ ​ ​

​​

You can see that the cutting effect is much better,But this still can't handle the oblique situation.

 

 ​http://www.waitingfy.com/archives/3778​​

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved