Picture

We represent pictures as a list of Pixel objects.

You may construct a blank picture object with a given width and height.

You may also load a picture object from an image file.

Example Usage

# Create a 600x400 picture.
p1 = Picture(600, 400)
# Set all its pixels to the color alice_blue.
p1.setToColor(alice_blue)
# Show it on the screen.
p1.show()

# Load the picture media/beach.png.
p2 = Picture('media/beach.png')
# Open it in the interactive picture explorer.
p2.explore()

Initialization

To initialize with a given width and height:

Picture.Picture(self, width, height)
Parameters:
  • width (int) – number of pixels wide the picture will be.
  • height (int) – number of pixels tall the picture will be.
# Create a blank 500x500 picture.
p = Picture(500, 500)

To initialize from a file:

Picture.Picture(self, filename)
Parameters:filename (string) – the filename of the image file.
# Load the picture 'beach.png' that lives in the 'media' folder.
p = Picture('media/beach.png')

Methods

This function displays the picture in a window.

Picture.show(self)
picture.show()

This function displays the picture in an interactive window which lets you click on and examine the pixels’ color values.

Picture.explore(self)
picture.explore()

This function will write your picture to a file.

Picture.write(self, filename)
Parameters:filename (string) – desired filename of your saved picture.
picture.write('mypicture.png')

Getters

This method returns the picture’s width.

Picture.getWidth(self)
width = picture.getWidth()

This method returns the picture’s height.

Picture.getHeight(self)
width = picture.getHeight()

This method returns the list of pixel objects in the picture.

Picture.getPixels(self)
pixels = picture.getPixels()

This method returns the pixel at location x, y.

Picture.getPixel(self, x, y)
Parameters:
  • x (int) – x-location of desired pixel.
  • y (int) – y-location of desired pixel.
# Get the pixel at location (x=50, y=100)
pixel = picture.getPixel(50, 100)

Table Of Contents