Python一些技巧

记录python一些技巧

1、使用if a is not b,而非if not a is b
2、不用if len(somelist) == 0来检查空值,而用if not somelist来检查(它会假定:空值将自动评估为False)。
3、检测是否为非空值时,用if somelist会默认把非空值判断为True
4、引入模块的时候,总是应该使用绝对名称,而不应该根据当前模块的路径来使用相对名称。例如,引入bar包中的foo模块时,应该完整写出from bar import foo。如果一定要用相对名称来编写import语句,那就采用明确的写法:from.import foo。import语句按顺序分三部分,分别代表标准库模块、第三方模块及自用模块,在每一部分中,各import语句应该按模块的字母顺序来排序。

vertical projection for python-opencv

关于垂直投影法的python,opncv的实现方式

关于垂直投影的具体介绍,就不多说,直接上代码


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
import cv2
import numpy as np

# 灰度化读取图片
image = cv2.imreade('number.jpg',0)

# 将图片二值化
retval, img = cv2.threshold(image,170,255,cv2.THRESH_BINAPY_INV)

# 创建一个空白图片(img.shape[0]为height,img.shape[1]为width)
paintx = np.zeros(img.shape,np.uint8)

# 将新图像数组中的所有通道元素的值都设置为0
cv2.cv.Zero(cv2.cv.fromarray(paintx))

# 创建width长度都为0的数组
w = [0]*image.shape[1]

# 对每一行计算投影值
for x in range(image.shape[1]):
for y in range(image.shape[0]):
t = cv2.cv.Get2D(cv2.cv.fromarray(img),y,x)
if t[0] == 0:
w[x] += 1

# 绘制垂直投影图
for x in range(image.shape[1]):
for y in range(w[x]):
# 把为0的像素变成白
cv2.cv.Set2D(cv2.cv.fromarray(paintx),y,x(255,255,255,0))

# 显示图片
cv2.imshow('paintx',paintx)
cv2.waitKey(0)

另外把水平投影也一起写了

1
2
3
4
5
6
7
8
9
10
11
12
13
painty = np.zeros(img.shape,np.uint8)
cv2.cv.Zero(cv2.cv.fromarray(painty))
h = [0]*image.shape[0]
for y in range(image.shape[0]):
for x in range(image.shape[1]):
s = cv2.cv.Get2D(cv2.cv.fromarray(img),y,x)
if s[0] == 0:
h[y] += 1
for y in range(image.shape(0)):
for x in range(h[y]):
cv2.cv.Set2D(cv2.cv.fromarray(painty),y,x,(255,255,255,0))
cv2.imshow('painty',painty)
cv2.waitKey(0)

amf for python

Python爬取FLASH播放器中的资料。

一、首先了解一下AMF协议:

AMF(Action Message Format)是Flash与服务端通信的一种常见的二进制编码模式,其传输效率高,可以在HTTP层面上传输。现在很多Flash WebGame都采用这样的消息格式。
AMF协议是基于Http协议的.
它的内容处理过程大致是这样:
1.从客户端获取Http请求(Request)流.
2.对流进行解串行化(Deserialize),得到服务器端程序能够识别的数据,并建立一个响应(Response)消息
3.Debug开始
4.对流进行各种处理(记录、许可、服务)得到返回值
5.对响应流进行串行化
6.发送Http响应给客户端

,