一、指定解释器
#!/usr/bin/python
调用 /usr/bin/ 下的 python 解释器
#!/usr/bin/env python
自动去寻找 python 解释器(防止没有安装到 /usr/bin/ 目录的情况)
二、限制版本
if not sys.version_info[0] == 3:
sys.exit(“only support python 3”)
三、opencv 相关
import cv2
import numpy as np
height, width, channel = 720, 1280, 3
size = height, width, channel
img = np.zeros(size, dtype=np.uint8)
img = cv2.rectangle(img, (1, 0), (200, 300), (0,255,0), 3)
#cv2.imshow("image", img)
cv2.imwrite("./empty.jpg", img)
yv12 = cv2.cvtColor(img, cv2.COLOR_BGR2YUV_YV12)
Yuv420 格式
Nv21
排布为
Y Y …
Y Y …
Y Y …
Y Y …
V U …
V U …
即每四个(上下左右) Y 共用一组 V、U
NV12
排布为
Y Y …
Y Y …
Y Y …
Y Y …
U V …
U V …
即每四个(上下左右) Y 共用一组 U、V
YV12
Y Y …
Y Y …
Y Y …
Y Y …
U U …
V V …
I420
Y Y …
Y Y …
Y Y …
Y Y …
V V …
U U …
四、path
import os
import sys
# 返回路径的目录
# note:当用 ./xx.py 运行时,返回 ‘.’;当用 python xx.py 执行时返回空
print(os.path.dirname(sys.argv[0]))
# 返回绝对路径
print(os.path.abspath(sys.argv[0]))
os.path.realpath() #也是绝对路径,如果有软链接,会返回软链接的真实路径
# 返回绝对路径的目录
print(os.path.dirname(os.path.abspath(sys.argv[0])))
五、下划线开头
Python 中类或函数以下划线开头时,表示为在内部使用,使用 from name import * 导入是无法访问的,但是可以使用 from name import _xxx 导入
path.py
#!/usr/bin/python
class _Path(object):
x = "here/tmp/"
from path import _Path
print(_Path.x)