Solarian Programmer

My programming ramblings

Install OpenCV 4 with Python 3 on macOS Mojave

Posted on November 29, 2016 by Paul

Updated 17 February 2019

In this article, I will show you how to install OpenCV 4 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.2.

There is more than one way to install Python 3 on macOS. In the previous version of this article I used to recommend Miniconda, while this approach still works for OpenCV other Pythonlibraries like Matplotlib seem to be broken for now. At the time of this update, 2019, what I recommend is to install Python 3 with Homebrew and using pip from the above to install the libraries.

Start by installing the Command Line Tools for macOS. Please note, that you will need the Command Line Tools even if you’ve already installed Xcode. Open a Terminal and write:

1 xcode-select --install

Once the Command Line Tools are installed, we can install Homebrew:

1 /usr/bin/ruby -e "$(curl -fsSL"

Next, we’ll use the brew command to install the latest stable version of Python:

1 brew install python

After the above, you can invoke Python3 using the python3 command. This is what I see if I run python3 on my machine:

1 ~ $ python3
2 Python 3.7.2 (default, Feb 12 2019, 08:15:36)
3 [Clang 10.0.0 (clang-1000.11.45.5)] on darwin
4 Type "help", "copyright", "credits" or "license" for more information.
5 >>> quit()
6 ~ $

Next, let’s follow best practices and create a new Python environment in which we can install NumPy and OpenCV:

1 python3 -m venv work
2 source work/bin/activate

At this point, your prompt should indicate that you are using the work environment. You can read more about Python environment in the documentation.

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 work/bin/activate command.

We can install NumPy and OpenCV with:

1 pip install numpy
2 pip install opencv-python

As a side note, when you are in a new environment you can use python to invoke the Python interpreter, no need to use the version number.

At this point, you should have OpenCV 4 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 4 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, OpenCV with Python By Example by G. Garrido and P. Joshi:

Show Comments