Skip to content

Commit 54069db

Browse files
committed
update dotdict
1 parent b8551d2 commit 54069db

11 files changed

+396
-23
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ python的强大之处有很大的一方面在于它有各种各样非常强大
247247

248248
## [delegator](content/delegator.md)
249249

250+
## [graphic-verification-code](content/graphic-verification-code.md)
251+
252+
## [bunch](content/bunch.md)
253+
254+
## [halo](content/halo.md)
255+
250256
## [tools](content/tools.md)
251257

252258
## [Other_thing](content/other_thing.md)

code/A_dot_accessible_dictionary.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
3+
4+
class DotDict(dict):
5+
6+
def __init__(self):
7+
super(DotDict, self).__init__()
8+
self.__dict__ = self
9+
10+
11+
if __name__ == '__main__':
12+
13+
a = DotDict()
14+
a['name'] = 'windard'
15+
a.year = 24
16+
print a

code/bunch.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
## bunch
2+
3+
将字典转换为类对象使用,可以使用点来取得字典的值
4+
5+
其实自己实现的话
6+
7+
```
8+
# -*- coding: utf-8 -*-
9+
10+
11+
class DotDict(dict):
12+
13+
def __init__(self):
14+
super(DotDict, self).__init__()
15+
self.__dict__ = self
16+
17+
18+
if __name__ == '__main__':
19+
20+
a = DotDict()
21+
a['name'] = 'windard'
22+
a.year = 24
23+
print a
24+
25+
```
26+
27+
或者这样,这也是 `dotdict` 这个库的全部内容
28+
29+
```
30+
# -*- coding: utf-8 -*-
31+
32+
33+
class DotDict(dict):
34+
35+
__setattr__ = dict.__setitem__
36+
__getattr__ = dict.__getitem__
37+
__delattr__ = dict.__delitem__
38+
39+
40+
if __name__ == '__main__':
41+
42+
a = DotDict()
43+
a['name'] = 'windard'
44+
a.year = 24
45+
print a
46+
print a.name
47+
48+
```
49+
50+
而 bunch 的实现是这样的
51+
52+
```
53+
# -*- coding: utf-8 -*-
54+
55+
56+
class DotDict(dict):
57+
58+
def __getattr__(self, item):
59+
try:
60+
return self[item]
61+
except KeyError:
62+
raise AttributeError(item)
63+
64+
def __setattr__(self, key, value):
65+
try:
66+
self[key] = value
67+
except:
68+
raise AttributeError(key)
69+
70+
71+
def __delattr__(self, item):
72+
try:
73+
del self[item]
74+
except:
75+
raise AttributeError(item)
76+
77+
78+
if __name__ == '__main__':
79+
80+
a = DotDict()
81+
a['name'] = 'windard'
82+
a.year = 24
83+
print a
84+
print a.name
85+
86+
```
87+
88+
基本上是一个思路,然后再加了一些捕获异常和类型转换,更容易的转换为其他的数据结构。
89+

code/halo_demo.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# coding=utf-8
2+
3+
from halo import Halo
4+
5+
spinner = Halo(text='Loading', spinner='dots')
6+
spinner.start()
7+
8+
# Run time consuming work here
9+
# You can also change properties for spinner as and when you want
10+
11+
spinner.stop()

code/pil_captcha.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# coding=utf-8
2+
try:
3+
import Image, ImageDraw, ImageFont, ImageFilter
4+
except ImportError:
5+
from PIL import Image, ImageDraw, ImageFont, ImageFilter
6+
import random
7+
8+
9+
# 随机字母:
10+
def rndChar():
11+
return chr(random.randint(65, 90))
12+
13+
14+
# 随机颜色1:
15+
def rndColor():
16+
return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))
17+
18+
19+
# 随机颜色2:
20+
def rndColor2():
21+
return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))
22+
23+
24+
if __name__ == '__main__':
25+
# 240 x 60:
26+
width = 60 * 4
27+
height = 60
28+
image = Image.new('RGB', (width, height), (255, 255, 255))
29+
# 创建Font对象:
30+
font = ImageFont.truetype('Arial.ttf', 36)
31+
# 创建Draw对象:
32+
draw = ImageDraw.Draw(image)
33+
# 填充每个像素:
34+
for x in range(width):
35+
for y in range(height):
36+
draw.point((x, y), fill=rndColor())
37+
# 输出文字:
38+
for t in range(4):
39+
draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2())
40+
41+
# 模糊:
42+
image = image.filter(ImageFilter.BLUR)
43+
image.show()
44+
image.save('code.jpg', 'jpeg')

code/pil_convert.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
img.save(des, "JPEG", quality=80, optimize=True, progressive=True)
1212
except IOError:
1313
ImageFile.MAXBLOCK = img.size[0] * img.size[1]
14-
img.save(des, "JPEG", quality=80, optimize=True, progressive=True)
14+
img.save(des, "JPEG", quality=80, optimize=True, progressive=True)

code/verification.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# coding=utf-8
2+
3+
import gvcode
4+
5+
img, code = gvcode.generate((240, 60))
6+
7+
print code
8+
9+
img.show()
10+
img.save('verification.jpg')

content/PIL.md

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,48 +62,60 @@ contrast_pic.save("../images/test8.jpg")
6262

6363
遇到了问题,还有其他的在安装opencv的时候也遇到了一个问题。
6464

65-
`ImportError: The _imagingft C module is not installed`` error: Unable to find vcvarsall.bat`,算了。不然就可以用Python写验证码了。
65+
`ImportError: The _imagingft C module is not installed``error: Unable to find vcvarsall.bat`,算了。不然就可以用Python写验证码了。
6666

6767
[在Python中用PIL做验证码](http://www.zouyesheng.com/captcha.html)
6868

6969
```python
70-
71-
import Image, ImageDraw, ImageFont, ImageFilter
70+
# coding=utf-8
71+
try:
72+
import Image, ImageDraw, ImageFont, ImageFilter
73+
except ImportError:
74+
from PIL import Image, ImageDraw, ImageFont, ImageFilter
7275
import random
7376

77+
7478
# 随机字母:
7579
def rndChar():
7680
return chr(random.randint(65, 90))
7781

82+
7883
# 随机颜色1:
7984
def rndColor():
8085
return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))
8186

87+
8288
# 随机颜色2:
8389
def rndColor2():
8490
return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))
8591

86-
# 240 x 60:
87-
width = 60 * 4
88-
height = 60
89-
image = Image.new('RGB', (width, height), (255, 255, 255))
90-
# 创建Font对象:
91-
font = ImageFont.truetype('Arial.ttf', 36)
92-
# 创建Draw对象:
93-
draw = ImageDraw.Draw(image)
94-
# 填充每个像素:
95-
for x in range(width):
96-
for y in range(height):
97-
draw.point((x, y), fill=rndColor())
98-
# 输出文字:
99-
for t in range(4):
100-
draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2())
101-
# 模糊:
102-
image = image.filter(ImageFilter.BLUR)
103-
image.show()
104-
image.save('code.jpg', 'jpeg');
92+
93+
if __name__ == '__main__':
94+
# 240 x 60:
95+
width = 60 * 4
96+
height = 60
97+
image = Image.new('RGB', (width, height), (255, 255, 255))
98+
# 创建Font对象:
99+
font = ImageFont.truetype('Arial.ttf', 36)
100+
# 创建Draw对象:
101+
draw = ImageDraw.Draw(image)
102+
# 填充每个像素:
103+
for x in range(width):
104+
for y in range(height):
105+
draw.point((x, y), fill=rndColor())
106+
# 输出文字:
107+
for t in range(4):
108+
draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2())
109+
110+
# 模糊:
111+
image = image.filter(ImageFilter.BLUR)
112+
image.show()
113+
image.save('code.jpg', 'jpeg')
114+
105115
```
106116

117+
或者使用 `graphic-verification-code`
118+
107119
#### imageFilter
108120

109121
图片模糊效果。

content/graphic-verification-code.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
## graphic-verification-code
2+
3+
国人写的图形验证码库,使用 PIL ,还算好用,只有两个函数
4+
5+
- generate 生成图像和验证码,图像是 PIL Image 实例
6+
- base64 生成图像和验证码,图形是 Base64 编码过的
7+
8+
```
9+
def generate(self, size=(120, 30), chars=None, format='PNG', mode='RGB', bg_color=(255, 255, 255), fg_color=(0, 0, 255), font_size=18, font_file=None, length=4, draw_lines=True, line_range=(1, 2), draw_points=True, point_chance=2):
10+
11+
"""
12+
@param size: 图片的大小,格式(宽,高),默认为(120, 30)
13+
@param chars: 允许的字符集合,格式字符串
14+
@param format: 图片保存的格式,默认为 PNG,可选的为 GIF,JPEG,TIFF,PNG
15+
@param mode: 图片模式,默认为 RGB
16+
@param bg_color: 背景颜色,默认为白色
17+
@param fg_color: 前景色,验证码字符颜色,默认为蓝色 #0000FF
18+
@param font_size: 验证码字体大小
19+
@param font_file: 验证码字体,默认为 None
20+
@param length: 验证码字符个数
21+
@param draw_lines: 是否划干扰线
22+
@param line_range: 干扰线的条数范围,格式元组,默认为 (1, 2),只有 draw_lines 为 True 时有效
23+
@param draw_points: 是否画干扰点
24+
@param point_chance: 干扰点出现的概率,大小范围 [0, 100],只有 draw_points 为 True 时有效
25+
@return: [0]: PIL Image 实例
26+
@return: [1]: 验证码图片中的字符串
27+
"""
28+
```
29+
30+
使用起来非常的简单
31+
32+
```
33+
# coding=utf-8
34+
35+
import gvcode
36+
37+
img, code = gvcode.generate()
38+
39+
print code
40+
41+
img.show()
42+
img.save('verification.jpg')
43+
44+
```
45+
46+
也可以直接使用 PIL 生成验证码
47+
48+
```
49+
# coding=utf-8
50+
try:
51+
import Image, ImageDraw, ImageFont, ImageFilter
52+
except ImportError:
53+
from PIL import Image, ImageDraw, ImageFont, ImageFilter
54+
import random
55+
56+
57+
# 随机字母:
58+
def rndChar():
59+
return chr(random.randint(65, 90))
60+
61+
62+
# 随机颜色1:
63+
def rndColor():
64+
return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))
65+
66+
67+
# 随机颜色2:
68+
def rndColor2():
69+
return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))
70+
71+
72+
if __name__ == '__main__':
73+
# 240 x 60:
74+
width = 60 * 4
75+
height = 60
76+
image = Image.new('RGB', (width, height), (255, 255, 255))
77+
# 创建Font对象:
78+
font = ImageFont.truetype('Arial.ttf', 36)
79+
# 创建Draw对象:
80+
draw = ImageDraw.Draw(image)
81+
# 填充每个像素:
82+
for x in range(width):
83+
for y in range(height):
84+
draw.point((x, y), fill=rndColor())
85+
# 输出文字:
86+
for t in range(4):
87+
draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2())
88+
89+
# 模糊:
90+
image = image.filter(ImageFilter.BLUR)
91+
image.show()
92+
image.save('code.jpg', 'jpeg')
93+
94+
```

0 commit comments

Comments
 (0)