Python Forum
Finding the brightness of an image using pygame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding the brightness of an image using pygame
#1
As part of my science fair, I need to order images by brightness, so I created this bit of code below:
#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()
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).
Reply
#2
It can actually be shorter using PIL/Pillow

from PIL import Image, ImageStat

def brightness_level(ifile):
   im = Image.open(ifile).convert('L')
   stat = ImageStat.Stat(im)
   return stat.mean[0]

images = [
'img1.jpg',
'img2.jpg',
'img3.jpg',
'img4.jpg',
'img5.jpg',
'img6.jpg'
]


for i in images:
    print('{} is {}'.format(i, brightness_level(i)))
Output:
img1.jpg is 37.026747637272514 img2.jpg is 40.700101538701865 img3.jpg is 33.871631648832306 img4.jpg is 10.251659915123456 img5.jpg is 102.22194878472222 img6.jpg is 213.753125
   
Although you would have to upload the images you were using to test if it came up with the same results as pygame.

Variations...


Convert image to greyscale, return average pixel brightness.

    def brightness( im_file ):
       im = Image.open(im_file).convert('L')
       stat = ImageStat.Stat(im)
       return stat.mean[0]
Convert image to greyscale, return RMS pixel brightness.

    def brightness( im_file ):
       im = Image.open(im_file).convert('L')
       stat = ImageStat.Stat(im)
       return stat.rms[0]
Average pixels, then transform to "perceived brightness".

    def brightness( im_file ):
       im = Image.open(im_file)
       stat = ImageStat.Stat(im)
       r,g,b = stat.mean
       return math.sqrt(0.241*(r**2) + 0.691*(g**2) + 0.068*(b**2))
RMS of pixels, then transform to "perceived brightness".

    def brightness( im_file ):
       im = Image.open(im_file)
       stat = ImageStat.Stat(im)
       r,g,b = stat.rms
       return math.sqrt(0.241*(r**2) + 0.691*(g**2) + 0.068*(b**2))
Calculate "perceived brightness" of pixels, then return average.

    def brightness( im_file ):
       im = Image.open(im_file)
       stat = ImageStat.Stat(im)
       gs = (math.sqrt(0.241*(r**2) + 0.691*(g**2) + 0.068*(b**2)) 
             for r,g,b in im.getdata())
       return sum(gs)/stat.count[0]
Recommended Tutorials:
Reply
#3
K, thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pygame Zero - no actor image toiling_away 2 1,753 Oct-14-2022, 12:16 AM
Last Post: toiling_away
  How to make an image move in relation to another in PYGAME CompleteNewb 1 2,280 Nov-10-2021, 03:38 PM
Last Post: metulburr
  open cv ->pygame: turn image into location Gamedeveloper 1 2,029 Jan-20-2020, 05:00 PM
Last Post: michael1789
  [PyGame] pygame image loading error BlueClaw 6 6,322 Dec-10-2019, 08:50 PM
Last Post: BlueClaw
  pygame uploading image from opengameart fatimabttt 3 2,950 Apr-16-2019, 08:50 PM
Last Post: nilamo
  [pyGame] Load Image, Problem Found ! JamieVanCadsand 2 8,686 Sep-29-2017, 06:26 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020