from Myro import * #Change all green pixels to white, and all other # pixels to black. def markGreen(myPic): for pix in getPixels(myPic): r = getRed(pix) g = getGreen(pix) b = getBlue(pix) if r < 70 and b < 70 and g > 100: setRed(pix,255) setGreen(pix,255) setBlue(pix,255) else: setRed(pix,0) setGreen(pix,0) setBlue(pix,0) #Return the percentage of the picture that is marked (green) #as a floating point number between 0.0 (0%) and 1.0 (100%) #Only works well on images created by markGreen (above) def pctMarked(pic): totalPixels = 0 whitePixels = 0 for pix in getPixels(pic): if getRed(pix) == 255: whitePixels = whitePixels + 1 totalPixels = totalPixels + 1 result = whitePixels / float( totalPixels) #float conversion for python2 return result #Find where the wall is located in the image (left/right/center) # Makes a number that represents the location of the center of # the white pixels (wall). Assumes the input image will be black # and white. def findAvgX(pic): pixelCount = 0 totalXCount = 0 for pix in getPixels(pic): if getRed(pix) == 255: x = getX(pix) totalXCount = totalXCount + x pixelCount = pixelCount + 1 avgX = totalXCount / float( pixelCount) #Float for python2 return avgX #Draws a vertical green line at a specified position. def drawVLine(pic, position): pHeight = getHeight(pic) for y in range(pHeight): pix = getPixel(pic,position,y) setRed(pix,0) setGreen(pix,255) setBlue(pix,0) #Test code: p = makePicture("rca1.gif") show(p, "Original") p2 = copyPicture(p) markGreen(p2) result = pctMarked(p2) print("{:.1f}% of the image is green".format(result*100) ) if result > 0.02: print("We found a green wall!") avgX = findAvgX(p2) print("Average X position is:", avgX) drawVLine(p2,int(avgX) ) show(p2, "result")