forked from qianlifeng/AutomaticPyJob
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryzation.py
More file actions
45 lines (37 loc) · 1021 Bytes
/
Binaryzation.py
File metadata and controls
45 lines (37 loc) · 1021 Bytes
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
34
35
36
37
38
39
40
41
42
43
44
45
#coding=gbk
from __future__ import unicode_literals
import Image
import urllib
class Binaryzation(object):
"""
将图像进行二值化处理
"""
image = None
def __init__(self,img):
self.image = img
def __ConvertToGray(self):
"""
将图片灰度化处理
"""
if self.image:
return self.image.convert('L')
return None
def ConvertToBinaryzation(self,b):
"""
二值化
b: 可以理解为亮度,大于这个亮度的全部变为纯白色
"""
if self.image:
self.image = self.__ConvertToGray()
return self.image.point(lambda i:i > b and 255)
return None
if __name__ == '__main__':
url = 'http://regcheckcode.taobao.com/auction/checkcode?sessionID=f06c56ea0e0bda9a9d71832422b68f29'
s = urllib.urlopen(url).read()
f = open('v.jpg','wb')
f.write(s)
f.close()
im = Image.open('v.jpg')
b = Binaryzation(im)
im = b.ConvertToBinaryzation(160)
im.show()