python 中的trick(二)

来源:互联网 发布:用ipad怎么开淘宝网店 编辑:程序博客网 时间:2024/06/06 08:25

0、Conda管理python

答:condaPIL是一个很不好装成功的库,因此选用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 Imageimage =Image.open('path/to/image')image.mode  #打印图像的格式(一般分为RGB[彩图],以及L[灰度图像])#L与RGB对应的转换公式为 #L = R * 299/1000 + G * 587/1000+ B * 114/1000gray_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

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)

原创粉丝点击