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.
# 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()
To initialize with a given width and height:
Parameters: |
|
---|
# Create a blank 500x500 picture.
p = Picture(500, 500)
To initialize from a file:
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')
This function displays the picture in a window.
picture.show()
This function displays the picture in an interactive window which lets you click on and examine the pixels’ color values.
picture.explore()
This function will write your picture to a file.
Parameters: | filename (string) – desired filename of your saved picture. |
---|
picture.write('mypicture.png')
This method returns the picture’s width.
width = picture.getWidth()
This method returns the picture’s height.
width = picture.getHeight()
This method returns the list of pixel objects in the picture.
pixels = picture.getPixels()
This method returns the pixel at location x, y.
Parameters: |
|
---|
# Get the pixel at location (x=50, y=100)
pixel = picture.getPixel(50, 100)