Title: CS1315: Introduction to Media Computation
1CS1315 Introduction to Media Computation
- How the transformations worked, and how to make
them better
2Understanding and using image manipulations better
- Lets 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
- Lets talk about how to avoid the degradation of
scaling
3Program 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,mirrorpointx,y)
- setColor(p2,getColor(p))
- show(source)
- return source
4Did it really work?
- It clearly did the mirroring, but that doesnt
create a 100 realistic image. - Check out the shadows Which direction is the sun
coming from?
5Understanding 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?
6Adding print statements to see whats 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",mirrorpointx,y
- p getPixel(source,mirrorpoint-x,y)
- p2 getPixel(source,mirrorpointx,y)
- setColor(p2,getColor(p))
- show(source)
- return source
7First pixels are either side of the mirrorpoint,
then moving down
- gtgtgt p2mirrorTemple()
- Copying color from 276 28
- to 278 28
- Copying color from 276 29
- to 278 29
- Copying color from 276 30
- to 278 30
8Counting 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,mirrorpointx,y)
- setColor(p2,getColor(p))
- count count 1
- show(source)
- print "We copied",count,"pixels"
- return source
9Counting pixels
- gtgtgt p2mirrorTemple()
- 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-14263
- 262 columns of pixels
- 70 262 18340
10Rotating the copy
def copyBarbSideways() Set up the source and
target pictures barbfgetMediaPath("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
11Rotating How it works
- We increment the same, but we use targetX for the
Y coordinate and targetY for the X coordinate
12Rotating How it works 2
- We increment sourceY and targetY just the same.
13Rotate How it ends
- Same amount of increment, even same values in the
variables, but a different result.
14Did this really Rotate?
- No, it flipped along the main diagonal.
- What do we need to do to really rotate (either
clockwise or counter-clockwise?)
15Rotating the copy
def rotateBarbSideways() Set up the source
and target pictures barbfgetMediaPath("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(b
arb)1) color getColor(getPixel(barb,sour
ceX,sourceY)) setColor(getPixel(canvas,targe
tY,width-targetX1), color) targetY
targetY 1 targetX targetX 1
show(barb) show(canvas) return canvas
16What 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.
17A 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(xlt0) or (ylt0) or (x gt getWidth(pic))
or (y gt 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)
Well see pass and else later, but you can
probably get a sense here of whats going on.
18Blurring out the pixelation