BYU logo Computer Science

To start this assignment, download this zip file.

Working with images

clouds

Understanding images

Pixels

  • 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.

RGB

  • 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')
    image.show()

In the zip file for this lesson, you will find a file called images.py. You can copy the above code and place it there to see how it works. Note that images.py 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 for...in 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:

  • pixel.red
  • pixel.green
  • pixel.blue

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:
        pixel.green = 0
        pixel.blue = 0


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

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:
        pixel.red = 255 - pixel.red
        pixel.green = 255 - pixel.green
        pixel.blue = 255 - pixel.blue


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

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 = pixel.red
        pixel.red = pixel.green
        pixel.green = pixel.blue
        pixel.blue = red


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

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:
        pixel.red = pixel.red * fraction
        pixel.green = pixel.green * fraction
        pixel.blue = pixel.blue * fraction
    return image


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

Grayscale

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 = (pixel.red + pixel.green + pixel.blue) / 3
        pixel.red = average
        pixel.green = average
        pixel.blue = average


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

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 * pixel.red + 0.769 * pixel.green + 0.189 * pixel.blue
        new_green = 0.349 * pixel.red + 0.686 * pixel.green + 0.168 * pixel.blue
        new_blue = 0.272 * pixel.red + 0.534 * pixel.green + 0.131 * pixel.blue
        # set new colors but be sure we don't go past 255
        pixel.red = new_red
        if pixel.red > 255:
            pixel.red = 255
        pixel.green = new_green
        if pixel.green > 255:
            pixel.green = 255
        pixel.blue = new_blue
        if pixel.blue > 255:
            pixel.blue = 255


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