BYU logo Computer Science

To start this assignment, download this zip file.

Working with images


Understanding images


  • Digital images consist of pixels
  • A pixel is a small square, showing a single color

Download and view pebbles.jpeg with an image viewer, zoom in, and see the pixels it uses.


  • each pixel can be controlled using a combination of red, green and blue
  • each color ranges from a value of 0 (minimum) to 255 (maximum)
  • these colors mix to form the color of the pixel
  • visit rgb-explorer to see color mixing in action

The byuimage library

  • code we have written to make it easier to work with images
  • similar to byubit, but in for images
  • visit your CS110 conda environment, and in a PyCharm terminal install this:
conda run -n cs110 python -m pip install byuimage

Loading an image

You can load an image from a file and then show it using the code below:

from byuimage import Image

if __name__ == "__main__":
    image = Image('mount-timpanogos.jpeg')

In the zip file for this lesson, you will find a file called You can copy the above code and place it there to see how it works. Note that should be in the same folder as mount-timpanogos.jpeg.

Looping over the pixels

  • the pixels in the image are a collection
  • this collection is ordered -> they go from left to right and top to bottom
    • (0,0) -> (1,0) -> (2,0) …
    • (0,1) -> (1,1) -> (2,1) …

image pixels

You an loop over all the pixels in an image using a loop:

for pixel in image:
  • In this loop, we now have a pixel variable that is set to the current pixel (from top to bottom, left to right)

Changing the color of pixels

You can then change the color of a pixel by setting values for:


Let’s set the blue and green values in each pixel of the image to 0. This will leave just the red values — the “red channel” of the image

from byuimage import Image

def change_to_red(image):
    for pixel in image: = 0 = 0

if __name__ == "__main__":
    image = Image('mount-timpanogos.jpeg')

Play around with the code

  • what about a green channel?
  • making all pixels black?

Modifying images

To continue exploring images, here are a variety of ways to modify an image.

Invert the pixels

We can invert the pixels of an image by subtracting their value from 255 (the maximum value).

from byuimage import Image

def invert_pixels(image):
    for pixel in image: = 255 - = 255 - = 255 -

if __name__ == "__main__":
    image = Image('mount-timpanogos.jpeg')

Swap red, green, blue colors

We can swap the red, green, and blue values for each pixel.

from byuimage import Image

def swap_pixels(image):
    for pixel in image:
        red = = = = red

if __name__ == "__main__":
    image = Image('mount-timpanogos.jpeg')

Darkening an image

You can darken an image by reducing each pixel by the same fraction:

from byuimage import Image

def darken(image, fraction):
    for pixel in image: = * fraction = * fraction = * fraction
    return image

if __name__ == "__main__":
    image = Image('mount-timpanogos.jpeg')
    darken(image, 0.5)


We can change an image to grayscale by setting every pixel to its average of red, green, and blue:

from byuimage import Image

def grayscale(image):
    for pixel in image:
        average = ( + + / 3 = average = average = average

if __name__ == "__main__":
    image = Image('mount-timpanogos.jpeg')

Sepia filter

We can change an image to sepia by using a special calculation:

from byuimage import Image

def sepia(image):
    for pixel in image:
        # calculate sepia values
        new_red = 0.393 * + 0.769 * + 0.189 *
        new_green = 0.349 * + 0.686 * + 0.168 *
        new_blue = 0.272 * + 0.534 * + 0.131 *
        # set new colors but be sure we don't go past 255 = new_red
        if > 255:
   = 255 = new_green
        if > 255:
   = 255 = new_blue
        if > 255:
   = 255

if __name__ == "__main__":
    image = Image('mount-timpanogos.jpeg')