'''CountColorOccurrences.py ''' IMG = pmOpenImage(0, "Earth.jpg") colorCounts = {} # Create an empty dictionary # Now use it to count the number of pixels of each color. for y in range(pmGetImageHeight(IMG)): for x in range(pmGetImageWidth(IMG)): color = pmGetPixel(IMG, x, y) # Get the pixel's color. try: count = colorCounts[color] # Retrieve current count. except KeyError: count = 0 # No count yet, make it zero. colorCounts[color] = count + 1 # Add one. itemList = colorCounts.items() print "This image has "+str(len(itemList))+" unique colors." itemList.sort(key=lambda item: -item[1]) print "The most common color is "+str(itemList[0][0]) print " which occurs "+str(itemList[0][1])+" times." print "Here is the complete list of colors and their counts:" print itemList