mac python ImportError:No module named cv2

张映 发表于 2018-12-18

分类目录: 技术其他

标签:, ,

玩了一下图片隐藏水印,需要用到opencv3,但是mac用这个玩意稍微有点麻烦

1,安装opencv3

$ brew install opencv3       //二选一
$ pip install opencv-python  //二选一

二种安装方式,产生的cv2.so路径不一样的,自己找下,以下配置,采用的是后者。

2,配置包加载

$ python
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> site.getsitepackages()  //查看当前程序使用包的路径
['/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python', '/Library/Python/2.7/site-packages']

$ cd /Library/Python/2.7/site-packages
$ sudo ln -s /usr/local/lib/python2.7/site-packages/cv2/cv2.so ./   //cv2.so软连到/Library/Python/2.7/site-packages

$ python
Python 2.7.10 (default, Feb  7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2     //如果不报错,就说明成功了
>>>

如果报 ImportError: numpy.core.multiarray failed to import,解决办法同上

$ sudo ln -s /usr/local/lib/python2.7/site-packages/numpy ./
$ sudo ln -s /usr/local/lib/python2.7/site-packages/numpy-1.15.4.dist-info ./


转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/other/2003.html