Feb-21-2019, 02:48 AM
As part of my science fair, I need to order images by brightness, so I created this bit of code below:
The premise behind it is that it blits an image to the screen and then goes pixel by pixel and finds the "brightness". It does this by averaging the RGB values (A white pixel would return 255, while a black would return 0). It then puts all those values in a list and averages that out. For whatever reason, one image, which is visibly brighter than another, outputs a lower score. Why? Is there a bug in my code? Or do I need to scrap it and re-write the whole thing?
Please reply ASAP, the Science Fair is due Friday (I may or may not have procrastinated).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
#imports import pygame from pygame. locals import * pygame.init() #images to test for brightness TESTS = [ pygame.image.load( "T1-0.png" ), pygame.image.load( "T1-1.png" ), pygame.image.load( "T1-2.png" ), pygame.image.load( "T1-3.png" ), pygame.image.load( "T2-0.png" ), pygame.image.load( "T2-1.png" ), pygame.image.load( "T2-2.png" ), pygame.image.load( "T2-3.png" ), pygame.image.load( "T3-0.png" ), pygame.image.load( "T3-1.png" ), pygame.image.load( "T3-2.png" ), pygame.image.load( "T3-3.png" )] #testing loop for image in TESTS: #set up display screen = pygame.display.set_mode((image.get_width(),image.get_height())) screen.blit(image,( 0 , 0 )) pygame.display.update() #reset value value = [] #loop to test each pixel for x in range (image.get_width()): for y in range (image.get_height()): #find brightness color = screen.get_at((x, y)) color = (color[ 0 ] + color[ 1 ] + color[ 2 ]) / 3 #add brightness to the value of the image value.append(color) #average the value of the image and display it to user print ( sum (value) / len (value)) #end program pygame.quit() quit() |
Please reply ASAP, the Science Fair is due Friday (I may or may not have procrastinated).