python - 标准化图像的亮度
问题描述
def normalize_brightness(img: Image) -> Image:
"""
Normalize the brightness of the given Image img by:
1. 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
2. find the factor, let's call it x, which we can multiply the
average brightness by to get the value of 128
3. 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.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 = h / (img_width * img_height)
x = int(128 // total_avg)
for i in range(img_width):
for j in range(img_height):
r, g, b = pixels[i, j]
pixels[i, j] = (min(255, r * x), min(255, g * x), min(255, b * x))
return img
所以基本上这就是我对以下功能的看法,但由于某种原因它不起作用。我相信我的计算和步骤是文档字符串告诉我要做的,但我迷失了从这里去哪里。
解决方案
推荐阅读
- python-3.x - 安装 IJulia 的问题
- node.js - 我想删除一个帖子,但有一些我似乎无法弄清楚的错误。如何使用 DELETE 从数据库中删除数据?
- lambda - NetLogo 匿名程序的问题
- python - 在 Datagrip 中将 Snowflake 数据库连接到 R 和 Python
- ios - Xcode 11 Swift 5 - 无法安装应用程序(无法加载 info.plist 文件)
- android - Android grpc 错误:TLS ALPN 协商失败,协议:[grpc-exp,h2]
- excel - 文件保存读取正确但抛出没有描述的错误
- r - 从 stats.nba.com 抓取数据,curl::curl_fetch_memory(url, handle = handle) 中出现错误
- ballerina - 芭蕾舞女演员:神秘的“方法太大”错误
- adaptive-cards - 适当的自适应卡片输入.ChoiceSet 模板结构