RGBness of an image
February 7, 2011 5 Comments
A looked upon this image and thought, “what a bluish image!”
And then it suddenly came to my mind. What can be a quantitative method of telling that this image is a blue image? This was the scheme I devised:
- Get RGB value of every pixel in the image.
- Replace the pixel with a true RED, GREEN or BLUE pixel depending on the weight of these color in the pixel.
ie replace (120,50,20) with (255,0,0)
- Now calculate the weight of each RED, GREEN and BLUE color of the newly formed image.
- ( For RGB value with equal wight, the pixel is not replaced with true R or G or B color but with equal percentage of the color with equal weight. )
I wrote a small Python script to do the analysis task for me.
The image above obviously has high percentage of Blue color in it. The image replaced with true REG, GREEN, and BLUE color would look like:
(JPEG compression artifacts caused some irregular shades of red, green and blue.)
And the percentage of Red, Green and Blue are as follows:
Redness: 14.2866575845 %
Greenness: 1.2399911032 %
Blueness: 84.4733513123 %
So, quantitavely we can claim that the image is a BLUE image.
[The Python code: imageRGBness]
#!/usr/bin/python import Image R_count = 0 G_count = 0 B_count = 0 total_count = 0 def checkColor(R,G,B): global R_count, G_count, B_count, total_count out = (0,0,0) R_add=0 G_add=0 B_add=0 if R>=G: if R>=B: out = (R,0,0) R_add = 1 else: out = (0,0,B) B_add = 1 elif G>=B: out = (0,G,0) G_add = 1 else: out = (0,0,B) B_add = 1 if R==G and out == (R,0,0): out = (R,G,0) R_add = 0.5 G_add = 0.5 if R==B and out == (R,0,0): out = (R,0,B) R_add = 0.5 B_add = 0.5 if G==B and out == (0,G,0): out = (0,G,B) G_add = 0.5 B_add = 0.5 R_count = R_count + R_add G_count = G_count + G_add B_count = B_count + B_add return out def compareRGB(pix,x,y): pix_temp = pix for i in range(0,y): for j in range(0,x): color = pix_temp[j,i] R = color G = color B = color set = checkColor(R,G,B) pix[j,i] = set return pix def openImage(filename): global R_count, G_count, B_count, total_count im = Image.open(filename) size_of_image = im.size x = size_of_image y = size_of_image pix = im.load() compareRGB(pix, x, y) im.save("test_out.jpg") total_count = x * y print "Redness: ", R_count/total_count*100 print "Greenness: ", G_count/total_count*100 print "Blueness", B_count/total_count*100 #start the script openImage("test.jpg")
[image used : http://www.flickr.com/photos/sul4bh/5161722620/ ]