CS1315: Introduction to Media Computation How the transformations worked, and how to make them better This is an exploration lecture – things to explore: How to walk through mirroring more carefully and how to debug for understanding. An additional copying transformation – rotations How we can use blurring to get scaling to look better If time permits: Talk about other kinds of blurring (weighting so that you don’t mess up edges) Talk about how to do rotations OTHER THAN right angles
Understanding and using image manipulations better Let’s walk through mirroring in more detail Use the temple fixing example What was really going on there? More on what we can do with copying: Rotating Let’s talk about how to avoid the degradation of scaling
Program to mirror the temple def mirrorTemple(): source = makePicture(getMediaPath("temple.jpg")) mirrorpoint = 277 lengthToCopy = mirrorpoint - 14 for x in range(1,lengthToCopy): for y in range(28,98): p = getPixel(source,mirrorpoint-x,y) p2 = getPixel(source,mirrorpoint+x,y) setColor(p2,getColor(p)) show(source) return source
Did it really work? It clearly did the mirroring, but that doesn’t create a 100% realistic image. Check out the shadows: Which direction is the sun coming from? Consider other reasons why this worked as well as it did. Clouds in the sky?
Understanding the Temple Fix What is the very first transfer of pixels from and to? Which (x,y) pixel from? Which (x,y) pixel to? What is the second set of pixels? How many pixels get copied? Try to get people to make guesses here, first, before going on.
Adding print statements to see what’s happening def mirrorTemple(): source = makePicture(getMediaPath("temple.jpg")) mirrorpoint = 277 lengthToCopy = mirrorpoint - 14 for x in range(1,lengthToCopy): for y in range(28,98): print "Copying color from",mirrorpoint-x,y print "to",mirrorpoint+x,y p = getPixel(source,mirrorpoint-x,y) p2 = getPixel(source,mirrorpoint+x,y) setColor(p2,getColor(p)) show(source) return source
First pixels are either side of the mirrorpoint, then moving down >>> p2=mirrorTemple() Copying color from 276 28 to 278 28 Copying color from 276 29 to 278 29 Copying color from 276 30 to 278 30
Counting pixels def mirrorTemple(): source = makePicture(getMediaPath("temple.jpg")) mirrorpoint = 277 lengthToCopy = mirrorpoint - 14 count = 0 for x in range(1,lengthToCopy): for y in range(28,98): p = getPixel(source,mirrorpoint-x,y) p2 = getPixel(source,mirrorpoint+x,y) setColor(p2,getColor(p)) count = count + 1 show(source) print "We copied",count,"pixels" return source
Counting pixels >>> p2=mirrorTemple() We copied 18340 pixels Where did that come from? How many rows? Y goes from 28 to 98 = 70 rows of pixels How many columns? X goes from 1 to 277-14=263 = 262 columns of pixels 70 * 262 = 18340
Rotating the copy def copyBarbSideways(): # Set up the source and target pictures barbf=getMediaPath("barbara.jpg") barb = makePicture(barbf) canvasf = getMediaPath("7inX95in.jpg") canvas = makePicture(canvasf) # Now, do the actual copying targetX = 1 for sourceX in range(1,getWidth(barb)+1): targetY = 1 for sourceY in range(1,getHeight(barb)+1): color = getColor(getPixel(barb,sourceX,sourceY)) setColor(getPixel(canvas,targetY,targetX), color) targetY = targetY + 1 targetX = targetX + 1 show(barb) show(canvas) return canvas Here’s another example of a transformation.
Rotating: How it works We increment the same, but we use targetX for the Y coordinate and targetY for the X coordinate
Rotating: How it works 2 We increment sourceY and targetY just the same.
Rotate: How it ends Same amount of increment, even same values in the variables, but a different result.
Did this really “Rotate”? No, it flipped along the main diagonal. What do we need to do to really rotate (either clockwise or counter-clockwise?)
Rotating the copy def rotateBarbSideways(): # Set up the source and target pictures barbf=getMediaPath("barbara.jpg") barb = makePicture(barbf) canvasf = getMediaPath("7inX95in.jpg") canvas = makePicture(canvasf) # Now, do the actual copying targetX = 1 width = getWidth(barb) for sourceX in range(1,getWidth(barb)+1): targetY = 1 for sourceY in range(1,getHeight(barb)+1): color = getColor(getPixel(barb,sourceX,sourceY)) setColor(getPixel(canvas,targetY,width-targetX+1), color) targetY = targetY + 1 targetX = targetX + 1 show(barb) show(canvas) return canvas Here’s another example of a transformation.
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.
A blurring recipe def blur(pic,size): for pixel in getPixels(pic): currentX = getX(pixel) currentY = getY(pixel) r = 0 g = 0 b = 0 count = 0 for x in range(currentX - size,currentX + size): for y in range(currentY - size, currentY + size): 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(pic,x,y)) g = g + getGreen(getPixel(pic,x,y)) b = b + getBlue(getPixel(pic,x,y)) count = count + 1 newColor = makeColor(r/count,g/count,b/count) setColor(pixel,newColor) We’ll see pass and else later, but you can probably get a sense here of what’s going on.
Blurring out the pixelation