我正在尝试沿着图像的水平线找到像素的亮度,但我坚持如何获取数字数组,或者可能使用循环来更改 x 值并输出像素的值,所以我可以计算每个的亮度,然后可以对该线进行平均。这是我到目前为止得到的:
from PIL import Image
from math import sqrt
import os
image = Image.open(os.path.join("mypath", "a.jpg"))
import numpy as np
image = imag.convert ('RGB')
X,Y = (137, 137) #this is where i dont know what else to add
pixelRGB = imag.getpixel((X,Y))
R,G,B = pixelRGB
brightness =([R,G,B])/3
print(brightness)
我不知道如何添加循环。
你应该用循环从 0 迭代到宽度
from PIL import Image
image = Image.open("1.png")
width, height = image.size
y = 137
brightness_values = []
# Loop through the horizontal line
for x in range(width):
pixelRGB = image.getpixel((x, y))
R, G, B = pixelRGB
brightness = (R + G + B) / 3
brightness_values.append(brightness)
print(brightness_values)
average_brightness = sum(brightness_values) / len(brightness_values)
print(f"Average brightness: {average_brightness}")