Pictures on Pictures

Consider the following problem: You want to copy an image in the top-left corner of an empty canvas. This might be part of a collage program or a family photo program that automatically combines

For this problem, we need to consider two pictures. We'll call the one being drawn on targetPic and the one being copied pic.

In-Class Problem: Below is a skeleton of the solution. Sketch out (don't use the compuer) the code within the nested loop. Hint: You'll need to call getPixel twice, getColor once, and setColor once:

file= pickAFile()
pic= makePicture(file)
targetPic= makeEmptyPicture(300,300)

# now draw pic in the top left corner of targetPic
col=1
while col<getWidth(pic):
    row=1
    while row<getHeight(pic):
                  # here is where we should grab a pixel from pic and draw it on targetPic





                  row=row+1
         col=col+1

Now suppose we want to draw the picture on the canvas, but not beginning at coordinate (1,1) of the target, but at (100,100). How would we fix our code?

    The solution entails introducing the variables targetCol and targetRow. They will change along with col and row, but they will begin at 100,100.

    Instructor DEMO

Debugging

    printNow('col:'+str(col))

Error Checking

The code above will break if the inner picture is too big. How could we check to make sure this doesn't happen? How could we set things up so it doesn't happen?

Instructor DEMO

Picture Frame

How is picture frame problem different from the samples in this lesson?

Attachments (4)

  • drawOnEmpty.py - on Apr 21, 2009 9:43 AM by David Wolber (version 3 / earlier versions)
    1k Download
  • drawOnEmptyChecked.py - on Apr 21, 2009 9:48 AM by David Wolber (version 1)
    1k Download
  • drawOnEmptyComplete.py - on Apr 21, 2009 9:44 AM by David Wolber (version 3 / earlier versions)
    1k Download
  • gore_skyscraper_2.jpg - on Apr 21, 2009 10:07 AM by David Wolber (version 1)
    56k View Download

Recent site activity