For this project, you create an Image object by passing the Image constructor two things:
An "anchor point" where the image should be centered
The name of the file containing the image
Since you are not going to be drawing the image on the screen, the anchor point is not important. You can just create a random Point (e.g. Point(0,0) and pass it to the constructor.
Sample usage:
img = Image(Point(0,0), 'somefile.gif')
Getting the dimensions of an image
If you have an Image object, you can call the getWidth or getHeight methods to return its width or height.
Sample usage (using our object from the previous example):
area = img.getWidth() * img.getHeight()
Reading the color values of a pixel
To get the color values of a pixel, you can call getPixel. You will need to pass it the x and y coordinates of the pixel you are interested in. The top left pixel is at (0,0).
GetPixel will return a list of 3 numbers, all between 0 and 255. The first number is the red value, the second is the green value, and the third is the blue value.
Here is some sample code that prints the color value of the top left pixel in our image:
# Indices of the different color values
RED = 0
GREEN = 1
BLUE = 2
colors = img.getPixel(0,0)
print('Red =', colors[RED])
print('Green =', colors[GREEN])
print('Blue =', colors[BLUE])
Setting the color values of a pixel
To set the color values of a pixel, you can call setPixel. You will need to pass it the x and y coordinates of the pixel as well as a special string with the color information. You can create this string by first calling a function called color_rgb and passing it the desired red, green, and blue values.
Here is an example. In this example, we set the top left pixel in our image to blue:
colorstring = color_rgb(0, 0, 255) # blue
img.setPixel(0, 0, colorstring)