It can actually be shorter using PIL/Pillow
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.
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: