ICA: Modules and OpenCV basics

Author

Susan Eileen Fox

Published

September 26, 2025

Overview

In this activity, you will practice the basics of importing and using modules while exploring the math and random modules. You will then jump into using OpenCV to read and display images.

To hand in: You will use Github Assignment to set up this assignment, and to push your code to save it to the cloud, to hand it in.

Modules

In our readings, videos, and class notes, we discussed how to go about importing a module, which contains constants, functions, and other tools that we can load and use when we need them.

The code below imports the math module and the random module.

import math
import random

If you aren’t sure what tools are in a module, you can look up the documentation online. You can also get help with a module in Python, by using the help function. Try adding a call to help to your script above, and see what it produces.

help(math)

Create a new Python file in your project for this activity, and try copying this code and the previous into it. When you run it, read what the help function prints. You can also ask about specific functions or constants in a module. Add this example to your test script.

help(random.randint)

After reading through the description of the randint function, and experiment with how it works. Call it over and over until you completely understand what it produces.

Try this to hand in: Create a new code file, dice.py that contains a script that simulates the tossing of two typical six-sided dice (faces numbered from 1 to 6). Use random.randint to generate random values. Print the two separate die values, and the sum of the two.

Reading and displaying images

The script below reads in and displays a single image. Review Chapter 1 from your Vision readings if you need help understanding what each step does. Read the script carefully, and predict for yourself and your teammate what you think the program will do.

import cv2

image = cv2.imread('SampleImages/mushrooms.jpg')
cv2.imshow("My image", image)
cv2.waitKey()

Finally, run your copy of this program to see if your predictions were correct.

Try this to hand in: Create a new Python file, being sure to import cv2 at the top of the file. Choose five images from the SampleImages folder, and read each of the five into its own variable in your program. Then display each image, using the same window name, "Images". Call waitKey after each imshow so that the program pauses to show the current picture before it moves on to the next.

You have created a simple slideshow of just five images. Suppose you wanted to display many more than 5, like all the images in SampleImages or a folder of 100 vacation photos. You wouldn’t want to copy and paste the lines to read and display images that many times! It also isn’t good programming style. It is a truth in computer science that any time a program starts to be tedious and repetitive to create, it is time to find a better way. Repetitive programs are hard to debug and change. Being lazy as a coder, seeking the simplest, shortest solution, often leads to better code.

In the next activity we will return to this task and use better tools to let us efficiently display all the images in a folder

Experimenting with colors

The program below, included in your Github repo as, colorBackground.py, uses Numpy tools to construct a new image built from scratch. The zeros function creates an array of the size and data type we specify, so we can make a new image array without reading from a file! This program, as written, sets every pixel in the array to be a dark red color.

import cv2
import numpy as np

image = np.zeros((200, 200, 3), np.uint8)

image[:,:] = (0, 0, 128)

cv2.imshow("Color", image)
cv2.waitKey()
1
Asks for every row and every column of image to be set to the color tuple

Try this to hand in: Run this program to be sure what it does. Then make four copies of lines 6 to 9. You can comment out my original line by putting # at the start of the line (or use the keyboard shortcut listed under the Code menu as Comment with Line Comment).

For each copy, experiment with color values until you create the following:

  • A pleasing purple color
  • A dark blue-green color
  • A murky green with a hint of yellow
  • A pale orange color

Drawing on images

Chapter 1 of your Vision readings talks about the basic drawing tools OpenCV provides. You might want to open that document, or the OpenCV documentation, for assistance during this part.

Try this to hand in: Create a new Python file, and in it write a script to create a simple picture.

  • First, choose your background image, which could be one of the SampleImages, an image of your own that you copy into your PyCharm project, or a blank image with a background color of your choice
  • Next, choosing your own colors, size, and details, draw a smiling stick figure on your image using OpenCV’s drawing functions
  • Be sure to save your new picture to a new file

An extra challenge if you have time

Try any of these if you have time and interest:

  • Draw multiple objects in your picture, not just one
  • Pick a landmark (x, y) coordinate on your stick-figure person, and describe all other coordinates as offsets from that landmark. Once you have done that, you can move the whole stick figure around, or draw multiple copies, just by changing the starting landmark coordinates. Draw stick figures at random locations on your background image.
  • Make a simple animation with 3-4 frames by clearing and redrawing after each call to waitKey, with small differences between each frame.

What to hand in

You should have multiple Python files for this activity. At the very least, you must have:

  • A file containing your solution for the dice problem
  • A file containing the 5-image slideshow script
  • The modified colorBackground.py file
  • A file containing your stick figure script
  • (Optional) a file or files containing any of the optional challenges

Use commit and push to copy your code to Github to submit this work.