Getting Started

First we may want to install the python library

pip install --upgrade opencv-python

Then we can open a python shell and start using
import cv2

Reading the Image

I found this simple image somewhere on my laptop. It seemed like a good choice to begin editing.

Now I can import it from the same directory where I’m running my Python shell

img = cv2.imread('americanfootballgamewithoutoffocusplayersinthebackgrou.jpg')

img is a matrix of shade values. These are actually in BGR instead of RGB but since img is a numpy matrix we can do a neat manipulation to get it into RGB if we want.

img2 = img[:,:,::-1]

It works because img is a 3 dimensional array of height, width, and color. Basically that says leave the first 2 dimensions of the matrix alone, and then in the third dimension (aka color) use the “reverse” step to reverse the array.


Recall what we did above with numpy array indexing. We could also use indexing to crop the image. For example this crops off 100 pixels from either end of the width

img2 = img[:,100:300,:]

The cv2.imwrite() method can help us save the image, so we can see what it did.