1 / 9

CS 101: Introduction to Computing

CS 101: Introduction to Computing. Rotating and Blurring. Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert H. Sloan, University of Illinois at Chicago, 2005, for educational use. Rotating the copy. def copyTonksSideways():

Download Presentation

CS 101: Introduction to Computing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 101: Introduction to Computing Rotating and Blurring Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert H. Sloan, University of Illinois at Chicago, 2005, for educational use.

  2. Rotating the copy def copyTonksSideways(): # Set up the source and target pictures tonksFile=getMediaPath("tonks.jpg") tonks = makePicture(tonksFile) canvasf = getMediaPath("7inX95in.jpg") canvas = makePicture(canvasf) # Now, do the actual copying--swap X & Y! targetX = 1 for sourceX in range(1,getWidth(tonks)): targetY = 1 for sourceY in range(1,getHeight(tonks)): color = getColor(getPixel(tonks,sourceX,sourceY)) setColor(getPixel(canvas, targetY, targetX), color) targetY = targetY + 1 targetX = targetX + 1 show(tonks) show(canvas) return canvas

  3. Rotating: How it works • We increment the same, but we use targetX for the Ycoordinateand targetY for the X coordinate

  4. Rotating: How it works 2 • We increment sourceY and targetY just the same.

  5. Rotate: How it ends • Same amount of increment, even same values in the variables, but a different result.

  6. What to do about scaling? • How do we clear up the degradation of scaling up? • Variety of techniques, but mostly following the same basic idea: • Use the pixels around to figure out what color a new pixel should be, then somehow (e.g., by averaging) compute the right color. • Different techniques look at different pixels and compute different averages in different ways.

  7. A blurring program def blur(pic,nNeighbors): original = makeDuplicate(pic) for pixel in getPixels(pic): currentX = getX(pixel) currentY = getY(pixel) r = 0 g = 0 b = 0 count = 0 for x in range(currentX - nNeighbors, currentX + nNeighbors +1): for y in range(currentY - nNeighbors, currentY + nNeighbors + 1): if (x<0) or (y<0) or (x >= getWidth(pic)) or (y >= getHeight(pic)): pass # Skip if we go off the edge else: r = r + getRed(getPixel(original, x, y)) g = g + getGreen(getPixel(original, x, y)) b = b + getBlue(getPixel(original, x, y)) count = count + 1 newColor = makeColor(r/count,g/count,b/count) setColor(pixel,newColor) def makeDuplicate(pic): #creates new picture object identical to old and returns it duplicate = makeEmptyPicture(getWidth(pic), getHeight(pic)) for x in range(1, getWidth(pic)): for y in range(1,getHeight(pic)): color = getColor(getPixel(pic, x, y)) setColor(getPixel(duplicate, x, y), color) return duplicate Need to go to JES and look at this and ask lots of questions about it.

  8. Bean: Pixelated, then smooth

  9. Questions about Blur • What’s with the + 1 at the end of the range? • Explain construct else: and pass • Explain exact test in the if for when to pass • Explain why we need makeDuplicate!

More Related