Solarian Programmer

My programming ramblings

Install OpenCV 3 with Python 3 on macOS Mojave

Posted on November 29, 2016 by Sol

Updated 27 October 2018

In this article, I will show you how to install OpenCV 3 with Python 3 on macOS Mojave. Most tutorials I found online, including the OpenCV documentation, seem concerned only with Python 2.7.

MacOS comes by default with Python 2.7 which, at this point, receives only bug fixes and will be EOL by 2020. Python 3.x is the future and it is supported by all major Python libraries. In this tutorial, we’ll use the latest stable Python 3 release, Python 3.7.1.

There is more than one way to install Python 3 on macOS. From my experience, the easiest approach for beginners is to use a package manager like Miniconda. Select the 3.7 bash installer from the Miniconda download page. Once the download is finished, open a Terminal and launch the installer:

1 cd Downloads/
2 bash

For the most part, you can use the defaults suggested by the installer. Be careful when asked if you want Miniconda added to your PATH. If you chose yes, the Miniconda Python will shadow the system Python, so when you will write python in a Terminal you will launch Python 3.7 instead of the default 2.7. My advice is to add Miniconda to your PATH. If, at some point, you want to revert to 2.7, it is as simple as commenting the Miniconda line from your .bash_profile file.

Once the installation is finished, assuming you kept the installer defaults, you need to enable the new PATH settings. This can be achieved by closing and reopening your Terminal, or simply by writing:

1 cd ~
2 . .bash_profile

Now, you should have the conda command available in your PATH. A quick test is to run the conda info command. This is what I see in my case:

 1 ~ $ conda info
 3      active environment : None
 4        user config file : /Users/paul/.condarc
 5  populated config files :
 6           conda version : 4.5.11
 7     conda-build version : not installed
 8          python version :
 9        base environment : /Users/paul/miniconda3  (writable)
10            channel URLs :
18           package cache : /Users/paul/miniconda3/pkgs
19                           /Users/paul/.conda/pkgs
20        envs directories : /Users/paul/miniconda3/envs
21                           /Users/paul/.conda/envs
22                platform : osx-64
23              user-agent : conda/4.5.11 requests/2.19.1 CPython/3.7.0 Darwin/18.0.0 OSX/10.14
24                 UID:GID : 501:20
25              netrc file : None
26            offline mode : False
28 ~ $

Let’s follow best practices and create a new Python environment:

1 conda create -n myenv python=3.7
2 source activate myenv

At this point, your prompt should indicate that you are using the myenv environment. An environment allows you to have different versions of Python and libraries on the same machine. As an example, you could have a myenv environment where you’ve installed SciPy and a play environment where you’ve installed PyGame. The environments are completely separate from each other. This is useful if you want, for example, to experiment with the development version of Python or some other library, while keeping the stable versions separate.

Once an environment is activated, all the install commands will apply only to the current environment. By default, if you close your Terminal, the environment is deactivated. If you want to be able to use it, use the source activate myenv command.

Next, use the pip package manager to install OpenCV 3:

1 pip install opencv-python

At this point, you should have OpenCV 3 and Python installed on your Mac. We can write a small test program that will print the OpenCV version, load an image from the disk, convert the image to gray and show the result. Start by downloading the next image:

Over the Clouds test image

Save it as clouds.jpg. In the same folder where you’ve saved the above image, create a new file and write the next code:

 1 import cv2
 3 print("OpenCV version:")
 4 print(cv2.__version__)
 6 img = cv2.imread("clouds.jpg")
 7 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 9 cv2.imshow("Over the Clouds", img)
10 cv2.imshow("Over the Clouds - gray", gray)
12 cv2.waitKey(0)
13 cv2.destroyAllWindows()

Run the code with:

1 python

(You can close the two windows by pressing ESC!)

You should see something like in the next figure. By default, the last image (the gray one) will be over the first one. You need to move the window in order to see both images:

Python 3 OpenCV 3 test - convert image to gray

If you want to learn more about OpenCV and Python, I would recommend reading OpenCV with Python Blueprints by M. Beyeler:

or, for OpenCV C++, OpenCV By Example by P. Joshi:

Show Comments