我正在为我的函数使用四个输入:图片对象,x坐标,y坐标和正方形的高度/宽度。我想分别平均图片的所有红色,绿色和蓝色值。我遇到RGB像素的累加器变量问题并检查方块的边界。任何人都可以帮助我吗?
from imageTools import *
p1 = makePicture("flower.jpg")
def averageColor(pic, xCord, yCord, width):
rAcc = 0
gAcc = 0
bAcc = 0
for x in range(xCord, xCord + width):
for y in range(yCord, yCord + width):
picWidth = getWidth(pic)
picHeight = getHeight(pic)
if x <= picWidth and y <= picHeight:
pixel = getPixel(pic, xCord, yCord)
r = getRed(pixel)
g = getGreen(pixel)
b = getBlue(pixel)
rAcc = rAcc + 1
gAcc = gAcc + 1
bAcc = bAcc + 1
avgRed = r / rAcc
avgGreen = g / gAcc
avgBlue = b / bAcc
newColor = makeColor(avgRed, avgGreen, avgBlue)
return newColor
col1 = averageColor(p1, 0, 150, 100)
print(col1)
答案 0 :(得分:0)
你想使用&lt;而不是&lt; =比较索引,因为索引是基于零的。例如,如果图片是10像素宽,则9是最后一个像素的索引
您还要覆盖累积变量r,g和b。你需要做
r = r + getRed(...)
哦。此外,您似乎正在尝试使用xCoord和yCoord来获取像素而不是x和y变量