AddisCoder 2023 Week 2
Taught by Alex Krentsel (alex.krentsel@gmail.com), based on material from CS106A at Stanford University, and CSCI100 at Howard University.
Let's go to the slides and talk about how images are represented.
# We've written a simple image module for you to use. Let's import SimpleImage.
# Note: if this doesn't work, you need to make sure you've downloaded the simpleimage.py
# file, it's in the same folder as this notebook, and you've installed the Pillow library
# by running `python3 -m pip install --upgrade Pillow` in your terminal.
from simpleimage import SimpleImage
# Let's play around with it to see how it works.
# First, let's create a new image that's 10 pixels wide and 10 pixels tall.
image = SimpleImage.blank(10, 10)
# Let's print out the image.
print(image)
<simpleimage.SimpleImage object at 0x10479d480>
# Oops, that's not very useful. Let's show the image instead.
image.show()
# Let's make the image bigger.
image.show(resize_width=200)
# What is this thing? You learned about a lot of types, so what type is it?
print(type(image))
# It looks like a new type? Kind of. This is a special type that we made for you to use.
# We will learn more about how you can make your own types later on. For now, let's just
# accept on faith that this type has been made for you to use and not worry about how it
# was made.
<class 'simpleimage.SimpleImage'>
# Okay, let's start painting by setting some pixels.
# First, let's set the pixel at (0, 0) to be red.
image.set_rgb(0,0, 255, 0, 0)
image.show(resize_width=200)
# We can get the width/height of the image.
print("width:", image.width, "height:", image.height)
width: 10 height: 10
# Let's set a whole row of pixels to be red.
for x in range(10):
image.set_rgb(x, 0, 255, 0, 0)
image.show(resize_width=200)
# Let's set the entire image to be red.
for x in range(10):
for y in range(10):
image.set_rgb(x, y, 255, 0, 0)
image.show(resize_width=200)
# How about making a striped image?
for x in range(10):
for y in range(10):
if y % 2 == 0:
image.set_rgb(x, y, 255, 0, 0)
else:
image.set_rgb(x, y, 0, 255, 0)
image.show(resize_width=200)
# Let's make this more general. Let's make a function that makes a striped image.
def stripe_image(image):
for x in range(image.width):
for y in range(image.height):
if y % 2 == 0:
image.set_rgb(x, y, 255, 0, 0)
else:
image.set_rgb(x, y, 0, 255, 0)
image = SimpleImage.blank(10, 10)
image.show(resize_width=200)
stripe_image(image)
image.show(resize_width=200)
# We can draw manually on a canvas.
canvas = SimpleImage.blank(10,10)
canvas.show(resize_width=200)
# Make 2 eyes
print("Adding eyes")
canvas.set_rgb(3,3,0,0,0)
canvas.show(resize_width=200)
canvas.set_rgb(7,3,0,0,0)
canvas.show(resize_width=200)
# Make a nose
print("Adding a nose")
canvas.set_rgb(5,5,0,0,0)
canvas.show(resize_width=200)
# Make a mouth
print("adding mouth")
canvas.set_rgb(3,6,0,0,0)
canvas.set_rgb(4,7,0,0,0)
canvas.set_rgb(5,7,0,0,0)
canvas.set_rgb(6,7,0,0,0)
canvas.set_rgb(7,6,0,0,0)
# for x in range(3, 8):
# canvas.set_rgb(x,7,0,0,0)
canvas.show(resize_width=200)
Adding eyes
Adding a nose
adding mouth
# Finally, let's combine what we've learned to make a random image.
import random
random_image = SimpleImage.blank(10, 10)
for r in range(random_image.width):
for c in range(random_image.height):
# set the pixel at position (r, c) to be red
randr = random.randint(0, 255)
randg = random.randint(0, 255)
randb = random.randint(0, 255)
random_image.set_rgb(r, c, randr, randg, randb)
# show the image
random_image.show(resize_width=300)
# we can get the color of a particular pixel:
pixel = random_image.get_rgb(0, 0)
print(pixel)
[219, 85, 244]
You may be wondering, what is SimpleImage?
For this class, you can understand SimpleImage as a custom complex type that we've created for you to use for working with canvases and images. In future classes, you'll learn more about how to create your own custom types.