Skip to content

Commit d3ade99

Browse files
committed
图像二值化
1 parent 029a467 commit d3ade99

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

Captcha/Binaryzation.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,45 @@
1+
#coding=gbk
12

3+
from __future__ import unicode_literals
4+
import Image
5+
import urllib
26

37
class Binaryzation(object):
4-
pass
8+
"""
9+
将图像进行二值化处理
10+
"""
11+
12+
image = None
13+
14+
def __init__(self,img):
15+
self.image = img
16+
17+
def __ConvertToGray(self):
18+
"""
19+
将图片灰度化处理
20+
"""
21+
if self.image:
22+
return self.image.convert('L')
23+
return None
24+
25+
def ConvertToBinaryzation(self,b):
26+
"""
27+
二值化
28+
b: 可以理解为亮度,大于这个亮度的全部变为纯白色
29+
"""
30+
if self.image:
31+
self.image = self.__ConvertToGray()
32+
return self.image.point(lambda i:i > b and 255)
33+
return None
34+
35+
36+
if __name__ == '__main__':
37+
url = 'http://regcheckcode.taobao.com/auction/checkcode?sessionID=f06c56ea0e0bda9a9d71832422b68f29'
38+
s = urllib.urlopen(url).read()
39+
f = open('v.jpg','wb')
40+
f.write(s)
41+
f.close()
42+
im = Image.open('v.jpg')
43+
b = Binaryzation(im)
44+
im = b.ConvertToBinaryzation(160)
45+
im.show()

0 commit comments

Comments
 (0)