# Make a blank canvas, exactly the right size
w = getWidth(picture)
h = getHeight(picture)
canvas = makeEmptyPicture(w, h)
# For each pixel in the picture:
allOfThePixels = getPixels(picture)
for pixel in allOfThePixels:
# Find where the pixel is
x = getX(pixel)
y = getY(pixel)
# Find the corresponding pixel in the canvas
secondPixel = getPixel(canvas, x, y)
# Set the color of the corresponding pixel
setColor(secondPixel, getColor(pixel))
return canvas
This function just copies the pixels down the left side of the picture
def cloneLeft(picture):
# Make a blank canvas, exactly the right size
w = getWidth(picture)
h = getHeight(picture)
canvas = makeEmptyPicture(w, h)
for row in range(1, h+1):
pixel = getPixel(pic, 1, row)
secondPixel = getPixel(canvas, 1, row)
setColor(secondPixel, getColor(pixel))
return canvas
This function (shown in class) just copies the pixels down the middle of the picture
def cloneMiddle(picture):
# Make a blank canvas, exactly the right size
w = getWidth(picture)
h = getHeight(picture)
canvas = makeEmptyPicture(w, h)
mid = w/2
for row in range(1, h+1):
pixel = getPixel(pic, mid, row)
secondPixel = getPixel(canvas, mid, row)
setColor(secondPixel, getColor(pixel))
return canvas
This function copies the first four columns of pixels on the left side of the picture
def cloneLeftBand(picture):
# Make a blank canvas, exactly the right size
w = getWidth(picture)
h = getHeight(picture)
canvas = makeEmptyPicture(w, h)
#Copy column 1
for row in range(1, h+1):
pixel = getPixel(pic, 1, row)
secondPixel = getPixel(canvas, 1, row)
setColor(secondPixel, getColor(pixel))
#Copy column 2
for row in range(1, h+1):
pixel = getPixel(pic, 2, row)
secondPixel = getPixel(canvas, 2, row)
setColor(secondPixel, getColor(pixel))
#Copy column 3
for row in range(1, h+1):
pixel = getPixel(pic, 3, row)
secondPixel = getPixel(canvas, 3, row)
setColor(secondPixel, getColor(pixel))
#Copy column 4
for row in range(1, h+1):
pixel = getPixel(pic, 4, row)
secondPixel = getPixel(canvas, 4, row)
setColor(secondPixel, getColor(pixel))
return canvas
# Ugh. That's really repetitious.
What we want is a way to repeat our repetition (or loop our loop)....
The answer is to have a column loop outside the row loop
def cloneAll(picture):
# Make a blank canvas, exactly the right size
w = getWidth(picture)
h = getHeight(picture)
canvas = makeEmptyPicture(w, h)
# Nested loops: Note that the block is indented twice
# In other words, for each column, copy all the rows
# and for each row, "copy" all the pixels in it
for column in range(1, w+1):
for row in range(1, h+1):
pixel = getPixel(pic, column, row)
secondPixel = getPixel(canvas, column, row)
setColor(secondPixel, getColor(pixel))