Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

How to copy a picture explained in stages

def clone(picture):

# 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


  1. 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

  1. 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


  1. 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.

  1. What we want is a way to repeat our repetition (or loop our loop)....
  2. 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))



Link to this Page