Presentation is loading. Please wait.

Presentation is loading. Please wait.

Some Utility Functions If you know the name of the file, searching for it with pickAFile() feels tedious You can set and get a media folder (path) for.

Similar presentations


Presentation on theme: "Some Utility Functions If you know the name of the file, searching for it with pickAFile() feels tedious You can set and get a media folder (path) for."— Presentation transcript:

1 Some Utility Functions If you know the name of the file, searching for it with pickAFile() feels tedious You can set and get a media folder (path) for remembering a place where your media will be coming from (or going to)  setMediaPath() lets you pick a file in your media folder  getMediaPath(basefilename) lets you generate a complete filename out of only the last part

2 Example >>> setMediaPath() New media folder: C:\mediasources\ >>> getMediaPath("barbara.jpg") 'C:\\mediasources\\barbara.jpg' >>> barb=makePicture(getMediaPath("barbara.jpg"))

3 Blank files in mediasources file = makeEmptyPicture(100,100) will create a blank picture of size 101x101 (why not 100x100?) DO writePictureTo(file,”100x100.jpg”) to save the blank picture canvas = makePicture(getMediaPath(“100x100.jpg”)) gives you a JPEG canvas of 101x101 pixels

4 Edge detection Pixel is different from its neighbors => an edge Measure (criterion) of pixel differences ?  One possibility – compare B/W value of a pixel with those at the left and below  If differences are above threshold, then an edge (black), otherwise (white) For each pixel in picture1: get B/W values of the pixel, its left and below (how?) if differences of (pixel, left) and (pixel, below) > Threshold paint the pixel black otherwise, paint the pixel white

5 Edge detection code def edgeDetect(pic, tHold): wid = getWidht(pic) hgt = getHeight(pic) edge = makeEmptyPicture(wid, hgt) for x in range(1, wid): for y in range(1, hgt): t = getPixel(pic, x, y) l = getPixel(pic, x-1, y) b = getPixel(pic, x, y+1) thisBW = (getRed(t) +getGreen(t)+getBlue(t))/3 leftBW = (getRed(l) +getGreen(l)+getBlue(l))/3 belBW = (getRed(b) +getGreen(b)+getBlue(b))/3 if abs(thisBW-leftBW) > tHold and abs(thisBW-belBW) > tHold: setColor(getPixel(edge, x, y), black) else: setColor(getPixel(edge, x,y), white) show(edge)

6 Making a collage Could we do something to the pictures we copy in?  Sure! Could either apply one of those functions before copying, or do something to the pixels during the copy. Could we copy more than one picture!  Of course! Make a collage!

7 Making a collage Given a blank canvas  Copy flower1 at lower left corner of canvas  Copy flower2 next  Copy negated flower1  Copy flower2 with blue removed  Copy flower1, negated and red reduced How do I copy to lower left corner?  Upper left corner is easy  Need to utilize getHeight(canvas) and getHeight(flower1) How do I copy flower2 adjacent to flower1 ?

8 Copy to lower left corner targetX = sourceX targetY = sourceY + ?  ? : getHeight(canvas) – getHeight(flower1) sourceX sourceY flower1

9 Copy flower2 targetX = sourceX + getWidth(flower1) targetY = sourceY + ?  ? : getHeight(canvas) – getHeight(flower2) sourceX sourceY flower1flower2

10 Put them together #First picture, at left edge targetX=1 for sourceX in range(1, getWidth(flower1)): targetY=getHeight(canvas)-getHeight(flower1)-5 for sourceY in range(1,getHeight(flower1)): px=getPixel(flower1,sourceX,sourceY) cx=getPixel(canvas,targetX,targetY) setColor(cx,getColor(px)) targetY=targetY + 1 targetX=targetX + 1 #Second picture, 100 pixels over targetX= getWidth(flower1)# 100 for sourceX in range(1,getWidth(flower2)): targetY=getHeight(canvas)-getHeight(flower2)-5 for sourceY in range(1,getHeight(flower2)): px=getPixel(flower2,sourceX,sourceY) cx=getPixel(canvas,targetX,targetY) setColor(cx,getColor(px)) targetY=targetY + 1 targetX=targetX + 1

11 def createCollage(): flower1=makePicture(getMediaPath("flower1.jpg")) print flower1 flower2=makePicture(getMediaPath("flower2.jpg")) print flower2 canvas=makePicture(getMediaPath("640x480.jpg")) print canvas #First picture, at left edge targetX=1 for sourceX in range(1,getWidth(flower1)): targetY=getHeight(canvas)-getHeight(flower1)-5 for sourceY in range(1,getHeight(flower1)): px=getPixel(flower1,sourceX,sourceY) cx=getPixel(canvas,targetX,targetY) setColor(cx,getColor(px)) targetY=targetY + 1 targetX=targetX + 1 #Second picture, 100 pixels over targetX=100 for sourceX in range(1,getWidth(flower2)): targetY=getHeight(canvas)-getHeight(flower2)-5 for sourceY in range(1,getHeight(flower2)): px=getPixel(flower2,sourceX,sourceY) cx=getPixel(canvas,targetX,targetY) setColor(cx,getColor(px)) targetY=targetY + 1 targetX=targetX + 1 #Third picture, flower1 negated negative(flower1) targetX=200 for sourceX in range(1,getWidth(flower1)): targetY=getHeight(canvas)-getHeight(flower1)-5 for sourceY in range(1,getHeight(flower1)): px=getPixel(flower1,sourceX,sourceY) cx=getPixel(canvas,targetX,targetY) setColor(cx,getColor(px)) targetY=targetY + 1 targetX=targetX + 1 #Fourth picture, flower2 with no blue clearBlue(flower2) targetX=300 for sourceX in range(1,getWidth(flower2)): targetY=getHeight(canvas)-getHeight(flower2)-5 for sourceY in range(1,getHeight(flower2)): px=getPixel(flower2,sourceX,sourceY) cx=getPixel(canvas,targetX,targetY) setColor(cx,getColor(px)) targetY=targetY + 1 targetX=targetX + 1 #Fifth picture, flower1, negated with decreased red decreaseRed(flower1) targetX=400 for sourceX in range(1,getWidth(flower1)): targetY=getHeight(canvas)-getHeight(flower1)-5 for sourceY in range(1,getHeight(flower1)): px=getPixel(flower1,sourceX,sourceY) cx=getPixel(canvas,targetX,targetY) setColor(cx,getColor(px)) targetY=targetY + 1 targetX=targetX + 1 show(canvas) return(canvas)

12 Making a collage createCollage() too messy Any good way to simplify it ? Go back to what processes were involved => find any pattern to exploit ?  Copy flower1 at lower left corner of canvas  Copy flower2 next  Copy negated flower1  Copy flower2 with blue removed  Copy flower1, negated and red reduced  Sure! Repetition of ‘Copy’ Let us make a general picCopy() function What info do we need for picCopy() ?

13 LAB: Write picCopy() Write picCopy(fPic,tPic,x,y) function in Program Area, which copies fPic to tPic starting from (x,y) of tPic In Command Area, call picCopy() five times to create a collage on the right You need negate(), clearBlue(), decreaseRed()


Download ppt "Some Utility Functions If you know the name of the file, searching for it with pickAFile() feels tedious You can set and get a media folder (path) for."

Similar presentations


Ads by Google