python 中的trick(二)

0、Conda管理python

答:conda
PIL是一个很不好装成功的库,因此选用conda管理python包。conda list 发现本来就有PIL真是好啊

conda装cv2

conda install -c https://conda.anaconda.org/menpo opencv  #这个

1、类型互转

元组 转换为 str

tup.__str__()

2、PIL

图像的格式(Image.mode属性)

from PIL import Image
image =Image.open'path/to/image')
image.mode  #打印图像的格式(一般分为RGB[彩图],以及L[灰度图像])

#L与RGB对应的转换公式为 
#L = R * 299/1000 + G * 587/1000+ B * 114/1000
gray_image =image.convert("L")
image.convert("YCbCr")
image.convert("I")
image.convert("1")     #二值图像
image.convert("CMYK")  #印刷四分色
#转为灰度图

#得到某一个像素点的像素值(第12行78列的像素值):
image.getpixel((12,78))

打开保存图像(open()与save()函数)

image=Image.open("path/to/image")
image.save('my.png')  or image.save('my.jpg')  or ... other format

读取txt并保存为unicode


3、Python 是否需要安装cv2

当我们用brew install opencv 的时候,或者用apt-get install opencv 或者 yum install opencv 之后,会在安装目录下面有一个lib,其中lib下有python有2.7 文件夹。
这个文件夹下有一个site-packages,其中的cv2.so 就是import cv2 所需要的唯一东西

安装完opencv之后会有一个目录如下,这个一个简要版本
.
├── bin
├── include
│   ├── opencv
│   └── opencv2    
│       ├── ccalib
│       ├── core
│       │   ├── cuda
│       │   │   └── detail
│       │   ├── hal
│       │   └── utils
│       ├── datasets
│       ├── dnn
│       ├── face
│       ├── highgui
│       ├── ml
│       ├── objdetect
│       ├── stitching
│       │   └── detail
│       ├── structured_light
│       ├── superres
│       └── xphoto
├── lib
│   ├── pkgconfig
│   ├── python2.7
│   │   └── site-packages   #cv2.so 加到路径
│   └── python3.6
│       └── site-packages
└── share
    └── OpenCV
        ├── haarcascades
        └── lbpcascades
当直接导入会出现如下情况

RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa
此时执行 pip install -U numpy 即可( U 对应 - -upgrade)

4、python与caffe

当我们编译caffe的时候,编译完成,然后make pycaffe 也无报错甚至无warnings, 
接下来用python import caffe 的时候,居然发现 segment fault 或者 core dumped
  • 往往是因为你编译caffe用的 python (设为2.7.10),可执行文件
  • 与 链接Library版本不一致(不是2.7.10)。
  • 这里写图片描述
  • 解决办法 cmake -D CPU_ONLY=ON -D PYTHON_LIBRARY=/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib …
    找到对应python的Library

5-bash: pip: command not found

实际上是你没有安装pip

[安装pip分两步]:
1、 sudo curl -O https://bootstrap.pypa.io/get-pip.py
2、sudo python get-pip.py
[pip 升级]:
1、$ pip install --upgrade pip

6. ImportError: No module named Cython.Distutils

分一步:
1、pip install Cython

7. ImportError: numpy.core.multiarray failed to import

[这个是import cv2导致的错误]一般是numpy与opencv要求的版本不一致。
因此,需要找合适的numpy sudo pip2 install -U numpy
但是如果选择sudo pip install -U numpy 可能会不奏效。这两个可以都试试。

8. TypeError: init() got an unexpected keyword argument ‘syntax’

这是由于安装tensorflow的时候,google的protobuf最低版本要求是3,所以先升级protobuf。或者先卸载本地的protobuf裤。

9. python安装jupter

  • python2 -m pip install jupyter #pip管理python2,并安装jupyter

9.1 UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 4: ordinal not in range(128)

  • python2中jupter中存在编码问题,解决办法:LANG=zn jupyter-notebook

10. ERROR 小结

10.1 TypeError: list indices must be integers, not tuple

发生这种情况下一般是list的索引引发的,

如list = [[1 , 2 , 3, 4, 5], [2, 3, 4, 5, 6], [4, 5, 6, 7, 8]]
list[:,1] 访问就会出错
此时必须将 list 先转为 numpy 在进行行列问 
list = np.array(list)

11 python计时工具

start =  time.time()
do something 
end = time.time()
print('time consuming : {} Seconds'.format(end-start))  # 单位S
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值