答:conda
PIL是一个很不好装成功的库,因此选用conda管理python包。conda list 发现本来就有PIL真是好啊
conda装cv2
conda install -c https://conda.anaconda.org/menpo opencv #这个
元组 转换为 str
tup.__str__()
图像的格式(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
当我们用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)
当我们编译caffe的时候,编译完成,然后make pycaffe 也无报错甚至无warnings,
接下来用python import caffe 的时候,居然发现 segment fault 或者 core dumped
实际上是你没有安装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
分一步:
1、pip install Cython
[这个是import cv2导致的错误]一般是numpy与opencv要求的版本不一致。
因此,需要找合适的numpy sudo pip2 install -U numpy
但是如果选择sudo pip install -U numpy
可能会不奏效。这两个可以都试试。
这是由于安装tensorflow的时候,google的protobuf最低版本要求是3,所以先升级protobuf。或者先卸载本地的protobuf裤。
发生这种情况下一般是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)
start = time.time()
do something
end = time.time()
print('time consuming : {} Seconds'.format(end-start)) # 单位S