1
CS 177 Week 6 Recitation Slides
Scaling Drawing on images
Vector-based Vs. Bitmap graphical representation
2
Announcements EXAM 1
Wednesday 09/29 6:30p - 7:30p EE 129
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
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
Scaling up: How it works
Same basic setup as copying and rotating:
5
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
Scaling up: How it works 3
Black pixels gets copied once…
7
Scaling up: How it works 4
And twice…
8
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
29
Final QUESTIONS???