import colorsys ### See also: Detecting thresholds in HSV color space (from RGB) using Python / PIL ### http://stackoverflow.com/questions/4890373 img = [[(255, 255, 255),( 0, 0, 0),(127, 127, 127),(255, 127, 255)], [(255, 127, 127),(255, 255, 255),(255, 255, 255),(255, 255, 255)], [(255, 255, 255),(255, 255, 255),(255, 255, 255),(255, 255, 255)], [(255, 255, 255),(255, 255, 255),(255, 255, 255),(255, 255, 255)]] def join2dArr(arr, d, func): return d.join([d.join([func(col) for col in row]) for row in img]) def toValue(rgb): #return str(colorsys.rgb_to_hsv(*rgb)[2]) return str(int(max(rgb[0], rgb[1], rgb[2]))) def toLightness(rgb): #return '{0:d}'.format(int(colorsys.rgb_to_hls(*rgb)[1])) return str(int((max(rgb[0], rgb[1], rgb[2])+min(rgb[0], rgb[1], rgb[2]))/2)) ## See: python — measuring pixel brightness ## http://stackoverflow.com/questions/6442118 def toLuminance(rgb): return '{0:d}'.format(int((0.2126 * rgb[0]) + (0.7152 * rgb[1]) + (0.0722 * rgb[2]))) print 'Value: ' + join2dArr(img, '\t', toValue) print 'Lightness: ' + join2dArr(img, '\t', toLightness) print 'Luminance: ' + join2dArr(img, '\t', toLuminance)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question