BYU logo Computer Science

Image Reference

We have written byuimage, a simple library that helps you to manipulate images. Images are a collection of pixels:

image made of pixels

Each pixel has a red, green, and blue value that is an integer in the range 0 to 255.

You can visit rgb-explorer to see how these colors combine to form the color of the pixel.

Installing byuimage

The most reliable way to install byuimage is through PyCharm. Open the File->Settings (Windows) or File->Preferences (MacOS) menu and select Project. You should see a your Conda environment and a list of packages installed in that environment:

PyCharm settings

Click the + and then type in byuimage. Then click Install Package.

Importing the library

To use byuimage, you always need to import it using the following:

from byuimage import Image

Loading and showing images

To load an image from a file and then show it, do the following:

from byuimage import Image

image = Image('mount-timpanogos.jpeg')
image.show()

The command Image(filename) creates an image from the given file name. The command image.show will open a window on your computer showing the image.

Creating an image

You can create a new, blank (white) image using:

new_image = Image.blank(width, height)

Image dimensions

You can get the height and width of an image using:

image.height
image.width

Looping over Pixels

To loop over the pixels in an image, use a for loop:

for pixel in image:
    ...

You can also loop over the pixels using their (x, y) coordinates:

for y in range(image.height):
        for x in range(image.width):
            ...

Getting a Pixel

You can get a pixel at a given (x,y) coordinate using:

image.get_pixel(x,y)

Modifying Pixels

To modify a pixel, use:

pixel.red = 50
pixel.green = 100
pixel.blue = 150

Each pixel color can be an integer in the range 0 to 255.

For example, to modify all the pixels in an image, you can use this for loop:

for pixel in image:
    pixel.red = 50
    pixel.green = 100
    pixel.blue = 150

Cheat Sheat

from byuimage import Image
  • Load, create, and show
    • image = Image(file) — load an image from a file
    • image = Image.blank(width, height) — create a blank image with the given height and width
    • image.show() — shows the image
  • Width and height
    • image.width — returns the image width in pixels
    • image.height — returns the image height in pixels
  • Looping over pixels
    • for pixel in image: — loop over all pixels
    • for y in range(image.height): — loop over all pixel Y values
    • for x range(image.width): — loop over all pixel X values
  • Getting and modifying pixels
    • image.get_pixel(x, y) — get the pixel at the given (X, Y) coordinate
    • pixel.red — returns the red value of the pixel
    • pixel.green — returns the green value of the pixel
    • pixel.blue — returns the blue value of the pixel
    • pixel.red = value — sets the red value of the pixel to the given value (0 to 255)
    • pixel.green = value — sets the green value of the pixel to the given value (0 to 255)
    • pixel.blue = value — sets the blue value of the pixel to the given value (0 to 255)

Image Manipulations

Invert all pixels

for pixel in image:
    pixel.red = 255 - pixel.red
    pixel.green = 255 - pixel.green
    pixel.blue = 255 - pixel.blue

Swap red, green, blue portions of each pixel

for pixel in image:
    red = pixel.red
    pixel.red = pixel.green
    pixel.green = pixel.blue
    pixel.blue = red

Darken an image

for pixel in image:
    pixel.red = pixel.red * 0.5
    pixel.green = pixel.green * 0.5
    pixel.blue = pixel.blue * 0.5

Change green pixels to black

for pixel in image:
    average = (pixel.red + pixel.blue + pixel.green) / 3
    if pixel.green > average:
        pixel.red = 0
        pixel.blue = 0
        pixel.green = 0

Copy an image to a new image

To copy an original image into a new image, you can do the following:

original = Image("mount-timpanogos.jpeg")
copy = Image.blank(original.height, original.width)
for y in range(original.height):
    for x in range(original.width):
        original_pixel = original.get_pixel(x, y)
        copy_pixel = copy.get_pixel(x, y)
        copy_pixel.red = original_pixel.red
        copy_pixel.green = original_pixel.green
        copy_pixel.blue = original_pixel.blue