Session 8: Matrices - Module B: Matrices and Images

1. Overview

How can we use matrices in Python?
How are images stored on a computer?
How can we modify an image?

2. What are images?

Images displayed on a computer screen are actually a collection of dots of color, called pixels. If you look really closely at the screen, you will be able to see the individual pixels.

The collection of pixels that make up an image are stored as a matrix. You can change that matrix to make the computer display a different picture: changing how dark or bright something is or flipping an image upside down. The matrix we will be using is in black-and-white (also called grayscale) so we only have to worry about working with one color value; usually color images have a component for red, green, blue, and opacity in each matrix cell.

3. To Load an Image

  1. Download the program to your computer and open it in gedit.
  2. Download a couple of images from the datasets page and save them in your programs directory.
  3. Let the program know which image to load by updating the path passed to loadImage() in
  4. Start ipython and run The image you selected will be displayed!

Examine the values the make up your image by printing the image matrix. Add the line print img after you load the image and run the program again. Are greater pixel values brighter or darker? Comment out the print statement when you are finished.

4. Image Modification

Notice how the all pixel values fall between 0 and 255, with 0 being completely black and 255 being completely white. Try changing individual pixels near the corners:

img[5][5] = 0

You can do a lot more than just changing single pixels! Try to add, subtract, multiply, and divide the img matrix with different values. Remember that final values need to fall between 0 and 255, so keep the number small for best results. What happens to the image when you add to the matrix? How about when you subtract?

Up for a challenge? See if you can figure out how to invert the image! Do you think you can use loops to make the image fade gradually? Try it for yourself!

5. Image Transformation

Matrix multiplication is incredibly useful when working with images, and soon you'll get to use it to transform your image. With a 3x3 transform matrix, you can perform two-dimensional operations like shifting, scaling, mirroring, skewing, and even rotation! It works by multiplying 1x3 [x,y,1] pixel coordinates with the 3x3 transform matrix to receive transformed 1x3 coordinates. The pixel value at the original location can then be moved to the new location. In the code, the dot() function is performing the matrix multiplication.


Or in python:

newCoordinates =

Try it out for yourself by editing the transform matrix! For better results, try to keep values in the matrix small, between -2 and 2. Below, you will find the matrix equation for the transform. If you need to start fresh, copy the following identity matrix:

transform = array([
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1]])

6. Image Transformation: Rotation

Getting rotation to work properly is a bit more tricky. Remember the wavy sin(x) and cos(x) functions that you used earlier this week? They can be used to convert angles into lengths, just what we need to determine the new location of pixels. Copy the following code below your current transform matrix.

angle = radians(30)
transform = array([
    [cos(angle) , sin(angle), 0],
    [-sin(angle), cos(angle), 0],
    [0 , 0 , 1]])

You can adjust the rotation angle and rotation center using the variables at the top. If you're feeling adventurous, you can even change the transform matrix!

7. Remember to Save a Picture!

Save your modified and transformed image for your webpage! Then answer these questions.