CS 177 Week 6 Recitation Slides
description
Transcript of CS 177 Week 6 Recitation Slides
![Page 1: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/1.jpg)
1
CS 177 Week 6 Recitation Slides
Scaling Drawing on images
Vector-based Vs. Bitmap graphical representation
![Page 2: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/2.jpg)
2
Announcements EXAM 1
Wednesday 09/29 6:30p - 7:30p EE 129
![Page 3: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/3.jpg)
Scaling
Scaling a picture (smaller or larger) has to do with sampling the source picture differently When we just copy, we sample every pixel If we want a smaller copy, we skip some pixels
We sample fewer pixels If we want a larger copy, we duplicate some pixels
We over-sample some pixels
3
![Page 4: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/4.jpg)
Scaling Up: Growing the picture
To grow a picture, we simply duplicate some pixels
We do this by incrementing by 0.5, but only use the integer part.
4
>>> print int(1)1>>> print int(1.5)1>>> print int(2)2>>> print int(2.5)2
![Page 5: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/5.jpg)
Scaling up: How it works
Same basic setup as copying and rotating:
5
![Page 6: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/6.jpg)
Scaling up: How it works 2
But as we increment by only 0.5, and we use the int() function, we end up taking every pixel twice.
Here, the blank pixel at (0,0) in the source gets copied twice onto the canvas.
6
![Page 7: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/7.jpg)
Scaling up: How it works 3
Black pixels gets copied once…
7
![Page 8: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/8.jpg)
Scaling up: How it works 4
And twice…
8
![Page 9: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/9.jpg)
Scaling the picture Up def copyBarbsFaceLarger(): # 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 sourceX = 45 for targetX in range(100,100+((200-45)*2)): sourceY = 25 for targetY in range(100,100+((200-25)*2)): color =getColor(getPixel(barb,int(sourceX),int(sourceY))) setColor(getPixel(canvas,targetX,targetY), color) sourceY = sourceY + 0.5 sourceX = sourceX + 0.5 show(barb) show(canvas) return canvas
![Page 10: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/10.jpg)
Scaling up: How it ends up
We end up in the same place in the source, but twice as much in the target.
Notice the degradation: Gaps that weren’t there
previously Curves would get “choppy”:
Pixelated
10
![Page 11: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/11.jpg)
What to do?
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
11
![Page 12: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/12.jpg)
Scaling the picture downdef copyBarbsFaceSmaller():
# Set up the source and target picture barbf=getMediaPath("barbara.jpg") barb = makePicture(barbf)
canvasf = getMediaPath("7inX95in.jpg") canvas = makePicture(canvasf) # Now, do the actual copying sourceX = 45 for targetX in range(100,100+((200-45)/2)): sourceY = 25 for targetY in range(100,100+((200-25)/2)): color = getColor(getPixel(barb,sourceX,sourceY)) setColor(getPixel(canvas,targetX,targetY), color) sourceY = sourceY + 2 sourceX = sourceX + 2 show(barb)
show(canvas)return canvas
![Page 13: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/13.jpg)
Drawing on images
Sometimes you want to draw on pictures,to add something to the pictures. Lines Text Circles and boxes.
We can do that pixel by pixel, setting black and white pixels, but that’s tedious
13
![Page 14: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/14.jpg)
Let’s do that with some JES functions!
addText(pict,x,y,string) puts the string starting at position (x,y) in the picture
addLine(picture,x1,y1,x2,y2) draws a line from position (x1,y1) to (x2,y2)
addRect(pict,x1,y1,w,h) draws a black rectangle (unfilled) with the upper left hand corner of (x1,y1) and a width of w and height of h
addRectFilled(pict,x1,y1,w,h,color) draws a rectangle filled with the color of your choice with the upper left hand corner of (x1,y1) and a width of w and height of h
14
![Page 15: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/15.jpg)
Remember, in JES some colors are already defined
Colors defined for you already: black, white, blue, red, green, gray, lightGray, darkGray, yellow, orange, pink, magenta, and cyan
15
![Page 16: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/16.jpg)
Drawing lines on Carolina
def lineExample(): img = makePicture(pickAFile()) verticalLines(img) horizontalLines(img) show(img) return img def horizontalLines(src): for x in range(0,getHeight(src),5): for y in range(0,getWidth(src)):
setColor(getPixel(src,y,x),black) def verticalLines(src): for x in range(0,getWidth(src),5): for y in range(0,getHeight(src)):
setColor(getPixel(src,x,y),black)
We can use the color name “black” – it’s pre-defined for us.16
![Page 17: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/17.jpg)
Let’s see how to use these functions
def addABox(): beach = makePicture(getMediaPath("beach-smaller.jpg")) addRectFilled(beach,150,150,50,50,red) show(beach) return beach
17
![Page 18: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/18.jpg)
Let’s see how to use these functions
def littlepicture(): canvas=makePicture(getMediaPath("640x480.jpg")) addText(canvas,10,50,"This is not a picture") addLine(canvas,10,20,300,50) addRectFilled(canvas,0,200,300,500,yellow) addRect(canvas,10,210,290,490) return canvas
18
![Page 19: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/19.jpg)
Let’s think about something…
Look at that previous page: Which has a fewer number of bytes? The program that drew the picture The pixels in the picture itself.
It’s a no-brainer The program is less than 100 characters (100 bytes) The picture is stored on disk at about 15,000 bytes
19
![Page 20: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/20.jpg)
Vector-based vs. Bitmap Graphical representations
Vector-based graphical representations are basically executable programs that generate the picture on demand. Postscript, Flash, and AutoCAD use vector-based
representations Bitmap graphical representations (like JPEG, BMP, GIF)
store individual pixels or representations of those pixels. JPEG and GIF are actually compressed representations
20
![Page 21: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/21.jpg)
Vector-based representations can be smaller
Vector-based representations can be much smaller than bit-mapped representations
Smaller means faster transmission (Flash and Postscript)
21
![Page 22: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/22.jpg)
But vector-based has more value than that
Imagine that you’re editing a picture with lines on it. If you edit a bitmap image and extend a line, it’s just more bits.
There’s no way to really realize that you’ve extended or shrunk the line.
If you edit a vector-based image, it’s possible to just change the specification
Change the numbers saying where the line is Then it really is the same line
That’s important when the picture drives the creation of the product, like in automatic fabric cutting machines
22
![Page 23: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/23.jpg)
How are images compressed?
Sometimes lossless using techniques like run length encoding (RLE) Instead of this:
B B Y Y Y Y Y Y Y Y Y B B We could say “9 Y’s” like this:
B B 9 Y B B Lossy compression (like JPEG and GIF) loses detail,
some of which is invisible to the eye.
23
![Page 24: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/24.jpg)
When changing the picture means changing a program…
In a vector-based drawing package, changing the drawing is changing a program.
How could we reach in and change the actual program? We can using string manipulation
The program is just a string of characters We want to manipulate those characters, in order to manipulate
the program
24
![Page 25: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/25.jpg)
Building a programmed graphic
def greyEffect(): file = getMediaPath("640x480.jpg") pic = makePicture(file) # First, 100 columns of 100-grey grey = makeColor(100,100,100) for x in range(1,100): for y in range(1,100): setColor(getPixel(pic,x,y),grey) # Second, 100 columns of increasing greyness greyLevel = 100 for x in range(100,200): grey = makeColor(greyLevel, greyLevel, greyLevel) for y in range(1,100): setColor(getPixel(pic,x,y),grey) greyLevel = greyLevel + 1
# Third, 100 colums of increasing greyness, from 0 greyLevel = 0 for x in range(200,300): grey = makeColor(greyLevel, greyLevel, greyLevel) for y in range(1,100): setColor(getPixel(pic,x,y),grey) greyLevel = greyLevel + 1 # Finally, 100 columns of 100-grey grey = makeColor(100,100,100) for x in range(300,400): for y in range(1,100): setColor(getPixel(pic,x,y),grey) return pic
26
![Page 26: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/26.jpg)
Another Programmed Graphic
def coolpic(): canvas=makePicture(getMediaPath("640x480.jpg")) for index in range(25,1,-1): color = makeColor(index*10,index*5,index) addRectFilled(canvas,0,0,index*10,index*10,color) show(canvas) return canvas
27
![Page 27: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/27.jpg)
And another
def coolpic2(): canvas=makePicture(getMediaPath("640x480.jpg")) for index in range(25,1,-1): addRect(canvas,index,index,index*3,index*4) addRect(canvas,100+index*4,100+index*3,index*8,index*10) show(canvas) return canvas
28
![Page 28: CS 177 Week 6 Recitation Slides](https://reader036.fdocuments.in/reader036/viewer/2022070422/56816479550346895dd65b73/html5/thumbnails/28.jpg)
29
Final QUESTIONS???