CMU 15-112: Fundamentals of Programming and Computer Science
Class Notes: Image Processing


  1. Introduction
  2. Examples


  1. Introduction
    • Pixels
      On a computer, images are made up of pixels.

      Each pixel displays as a single color. When taken together, the pixels form the image.

    • RGB Representation
      While there are a variety of different standards for storing the color of a pixel, in this course we will use RGB values. With RGB, each pixel is represented by three numbers: The red, green, and blue components of the color. Each number number is between 0 and 255.

      Consider these sample colors:
      RGB Values Color
      [200,0,0]
      [0,200,0]
      [0,0,250]
      [50,50,50]
    • ImageWriter Library
      We have created a simple library to make it easier for you to work with images. In order to run any of the examples below, be sure to first download the ImageWriter library and put it in the same folder as the code you want to run.

    • Installing the OpenCV library
      In order to use the ImageWriter library, you will need to install another library called OpenCV. OpenCV is used to manipulate and analyze images, and our ImageWriter library makes use of it.

      To install in Thonny, go to Tools -> Manage Packages. Then search for "opencv-python" and you will see the following:


      Do not click install. Instead, click on the "..." and under desired version choose "4.1.2.30" and press install. (The latest version has a strange bug on some platforms, so we are avoiding it for now.)

  2. Examples
    Note that all of the following examples require you to operate an an image. I recommend you download souq_hd.jpg. (Note, this file is copyrighted, but allowed for use here. It is by Diego Delso, delso.photo, License CC-BY-SA.)

    Also note that none of these can run in the browser, you'll need to run them locally.
    • Converting an image to gray scale
      import ImageWriter # Load the image from a file mypic = ImageWriter.loadPicture("souq_hd.jpg") # Show the image on the screen ImageWriter.showPicture(mypic) # Get the width and height (in pixels) of the image width = ImageWriter.getWidth(mypic) height = ImageWriter.getHeight(mypic) # Iterate over every pixel, changing each to a shade of gray. # Note that the picture will NOT update on the screen automatically. for i in range(width): for j in range(height): colors = ImageWriter.getColor(mypic, i, j) theAverage = (colors[0]+colors[1]+colors[2])//3 ImageWriter.setColor(mypic,i,j,[theAverage,theAverage,theAverage]) # Update the picture on the screen. ImageWriter.updatePicture(mypic)
    • Swapping around colors
      import ImageWriter mypic = ImageWriter.loadPicture("souq_hd.jpg") ImageWriter.showPicture(mypic) width = ImageWriter.getWidth(mypic) height = ImageWriter.getHeight(mypic) for i in range(width): for j in range(height): colors = ImageWriter.getColor(mypic, i, j) colors = [colors[2],colors[0], colors[1]] ImageWriter.setColor(mypic,i,j,colors) ImageWriter.updatePicture(mypic)

  3. More Examples
    We will do a lot more examples in class.