270 likes | 477 Views
Week 3. Translating. Can we put a picture wherever we want it?. Another Useful Tool. makeEmptyPicture(width, height) Can you guess what it does? Can you guess what it returns?. Idea. Let's make up our own "picture" from scratch!. Where?. (xpos, ypos). Last Week.
E N D
Translating • Can we put a picture wherever we want it?
Another Useful Tool makeEmptyPicture(width, height) Can you guess what it does? Can you guess what it returns?
Idea • Let's make up our own "picture" from scratch!
Where? (xpos, ypos)
Last Week def drawSquare(posx, posy, picture, redVal): for x in range(1, 151): for y in range(1, 151): xdraw = x + posx ydraw = y + posy b = x * 1.7 g = y * 1.7 px = getPixel(picture, xdraw, ydraw) setColor(px, makeColor(redVal, g, b))
Testing It blank = makeEmptyPicture(750, 750) drawSquare(130, 45, blank, 0) drawSquare(400,400, blank, 255) show(blank)
Today 750 750
The "Main" Program def matrix(): pic = makeEmptyPicture(750, 750) r = 0.0 for y in range(1, 602, 150): for x in range(1, 602, 150): square(pic, x, y, int(r)) r = r + 10.625 printNow(r) return pic
Note! • The "main" program returns a picture. • This is useful. • It could be hard-coded to save the final picture as a file • As a general rule you want functions to be a general purpose and as reusable as possible • You can always "wrap" a function in another function to fulfill a specific purpose
Scaling • Can we make pictures bigger or smaller? • Can we stretch them?
Scaling • Want a function that will take in a picture and a "scale factor" as parameters and will return a new picture scaled by "scale factor" • Example: • halfsize = scale(picture, 0.5) • doublesize = scale(picture, 2.0) • What are halfsize and doublesize?
Scaling! oldx newx = oldx * scalefactor newx
Recipe • Take in picture and scale factor • Determine size of new picture • Make a new picture (empty) • Go through each pixel of new picture (hint 2 fors) • Determine pixel in old picture that corresponds to new picture and copy color over
Scaling! oldx newx = oldx * scalefactor newx
Combining Pictures • Blending
def blend(pic1, pic2): width = getWidth(pic1) height = getHeight(pic2) pic3 = makeEmptyPicture(width, height) for x in range(1, width+1): factor = x*1.0/width other = 1.0 - factor other = x * 1.0/width factor = 1.0 - other for y in range(1, height+1): r1 = getRed(getPixel(pic1, x, y)) r2 = getRed(getPixel(pic2, x, y)) newr = factor*r1 + other*r2 g1 = getGreen(getPixel(pic1, x, y)) g2 = getGreen(getPixel(pic2, x, y)) newg = factor*g1 + other*g2 b1 = getBlue(getPixel(pic1, x, y)) b2 = getBlue(getPixel(pic2, x, y)) newb = factor*b1 + other*b2 setColor(getPixel(pic3, x, y), makeColor(newr, newg, newb)) return pic3
Combining Pictures • Background Subtraction - =
Combining Pictures • Chromakey
Homework 3 Write a function named hw3 in a file named hw3.py to create a collage of the same image at least four times fit onto the 7x95in blank JPEG. This image "7inX95in.jpg" is in the media sources folder. (You are welcome to add additional images, too.) One of those four copies can be the original picture. The other three should be modified forms. You can do any of: Scaling, cropping, or rotating the image, shifting or altering colors on the image, and making it darker or lighter.
Homework 3 Each of your four images should be in different quadrants of the picture. One should be in the upper left, the next in the lower left, the next in the upper right, and the fourth in the lower right. You may use as many other images as you with, in any other places, but those four images in four different quadrants must be there.
Homework 3 Your single function should make all of this happen--all of the effects and compositing must occur from the single function hw2(). IT IS PERFECTLY OKAY FOR HW2() TO USE OTHER FUNCTIONS! When grading we will only setMediaPath() to the directory containing your image (or images) and then execute hw2()--and will expect to see a collage get generated. In other words hw2() must not take any arguments!
Homework 3 Turn in your code, your original image (or images), and your resultant image into WebWork as hw3 Note: this means that you must run your code and generate the final image and save it to a file to be turned in!
Hints • Test your code before turning it in! • Do not make changes after testing! • Make sure you turn in all files required • Read, understand and follow the instructions! • NOTE: Your submissions will be displayed at the OMED Banquet for everyone to see!
pickAFile() makePicture(file) getPixels(picture) getPixel(picture, x, y) getRed(pixel) getWidth(pic) writePictureTo(pic, file) setRed(pixel, value) pickAFile() makeSound(file) getSamples(sound) getSampleObjectAt(sound,1) getSampleValueAt(sound, index getLength getSamplingRate writeSoundTo(sound,”file.wav”) setSample(sample,value) getSample(sample) Sound