# # Jay Summet # CS 1301 - October 8th, 2008 # # Released to the public domain. # # Example code to find a green wall by marking all green pixels white and # all non-green pixels black. Then it calculates the average X position of # the white pixels. # from myro import * def markGreen( pixel): #Put a type check up here! r = getRed(pixel) g = getGreen(pixel) b = getBlue(pixel) if g > 125 and r < 125: setRed(pixel,255) setGreen(pixel,255) setBlue(pixel,255) else: setRed(pixel,0) setBlue(pixel,0) setGreen(pixel,0) def countMarked(pic): count = 0 for pix in getPixels(pic): r = getRed(pix) g = getGreen(pix) b = getBlue(pix) if (r == 255) and (g == 255) and (b == 255): count = count + 1 return(count) def avgXMarked(pic): count = 0 avg = 0.0 for pix in getPixels(pic): r = getRed(pix) g = getGreen(pix) b = getBlue(pix) if (r == 255) and (g == 255) and (b == 255): count = count + 1 avg = avg + getX(pix) try: avg = avg / count except: avg = -1 return(avg) def greenWall(pic): for pixel in getPixels(p): markGreen(pixel) numMarked = countMarked(p) if (numMarked > 2000): return True else: return False p = loadPicture("wall1.gif") show(p) for pixel in getPixels(p): markGreen(pixel) x = avgXMarked(p) show(p,"result") print "Average x is: ", x