def normalize_brightness(img: Image) -> Image: """
Normalize the brightness of the given Image img by:
computing the average brightness of the picture: - this can be done by calculating the average brightness of each pixel in img (the average brightness of each pixel is the sum of the values of red, blue and green of the pixel, divided by 3 as a float division) - the average brightness of the picture is then the sum of all the pixel averages, divided by the product of the width and height of img
find the factor, let's call it x, which we can multiply the average brightness by to get the value of 128.
multiply the colors in each pixel by this factor x """
img_width, img_height = img.size pixels = img.load() # create the pixel map h = 0 for i in range(img_width): for j in range(img_height): r, g, b = pixels[i, j] avg = sum(pixels[i, j]) / 3 h += avg total_avg = int(h / (img_width * img_height)) x = 128 // total_avg r, g, b = pixels[i, j] pixels[i, j] = (r * x, g * x, b * x) return img
I am a little lost as to what I am doing wrong can someone help?