''' Purpose: support image black-and-white transformation ''' def size( original ) : ''' Purpose: return the size of the new image when performing a monotone transformation ''' nw, nh = original.size # Use the original dimensions return (nw, nh) def color( p ) : ''' Returns a new pixel whose RRB values are either (0, 0, 0) or (255, 255, 255). Returns (0, 0, 0) if the average of RGB values for pixel p is closer to 0 then 255; otherwise, it returns (255, 255, 255) ''' # Black and white images use either black or white for the pixels depending on # whether the color's average is closer to black or white # If it's closer to white, use white! # If it's closer to black, use black! # It changes the pixel p to be black or white depending on what it's average is closer to. r,g,b = p # Extract r,g,b from the original pixel average = ( r + g + b ) // 3 # Check if the average is less than 127 (approx 255/2) and return (0,0,0) if ( average < (255//2) ): # Closer to 0 (less than half of 255) return (0,0,0) else: # Closer to 255 (greater than half of 255) return (255,255,255) def where( nspot, original = None ) : ''' Purpose: return the correspondent of nspot in the original when doing a monotone transformation ''' ospot = nspot # Use the original spots return ospot if ( __name__ == '__main__' ) : import url from manip import manip # get web image of interest link = "http://www.cs.virginia.edu/~cs1112/images/mandrill/full.png" original = url.get_image( link ) new_image = manip( original, size, color, where ) new_image.show()