SimpleImage Reference
Basic SimpleImage Functions
The simple CS106AP "SimpleImage" code provides basic digital image processing code for you to call.
Advanced SimpleImage Functions
- 1. Access image size: image has .width and .height attributes
image.width
- 2. Create a blank/white image, here width 200 height 100
image = SimpleImage.blank(200, 100)
- By default the image is white, every pixel (255, 255, 255). An optional third argument can specify a color for the whole image of 'black', 'red', 'green', 'blue' or 'white:
image = SimpleImage.blank(200, 100, 'black')
Here is an older syntax to create a blank/white image that also works:
image = SimpleImage('', width=200, height=100)
width and height numbers should be integers
- 3. Access 1 pixel by its x,y coordinates: image.get_pixel(x, y)
x=0 is the left column, growing toward the right
y=0 is the top row, growing towards the bottom
Here accessing pixel at x 10 and y 20:
pixel = image.get_pixel(10, 20)
- x value must be in the range 0..width-1 (inclusive)
- y value must be in the range 0..height-1 (inclusive)
- x and y numbers should be integers
Range Loop
The "foreach" above is the easiest way to loop over all the pixels.
However sometimes you want to write loops to access pixels by their x,y
coordinates. Here is the canonical nested range loops to access
all the pixels in an image. This loop does the top row (y=0), then the next
row (y=1) and so on.
def example(filename):
image = SimpleImage(filename)
for y in range(image.height):
fox x in range(image.width):
pixel = image.get_pixel(x, y)
# do something with pixel
pixel.red = pixel.red // 2
return image