我正在编写一个程序来更改图片各个部分的颜色。在本例中为图像的顶部和底部三分之一。
我可以更改底部三分之一,但由于某种原因程序无法识别 if (y<h/3)
我尝试用实际数字代替它,并改变了编码颜色变化的方式。
有人可以指出我犯的(可能非常明显的)错误吗?
def changeByThirds(pic):
w= getWidth (pic)
h = getHeight(pic)
newPic = makeEmptyPicture(w,h)
for x in range (0,w):
for y in range (0,h):
pxl = getPixel(pic, x, y)
if (y<h/3):
newPxl= getPixel (newPic, x, y)
color = makeColor(getRed(pxl)*0.1, getGreen(pxl), getBlue(pxl))
setColor ( newPxl, color)
if (y>(h*2/3)):
newPxl= getPixel (newPic, x, y)
color = makeColor(getRed(pxl), getGreen(pxl), 0)
setColor ( newPxl, color)
else:
newPxl = getPixel (newPic, x, y)
color = getColor(pxl)
setColor ( newPxl, color)
return (newPic)
def do():
file = pickAFile()
pic = makePicture(file)
newPic = changeByThirds(pic)
writePictureTo(newPic, r"D:\FOLDER\0pic3.jpg")
explore (newPic)
最佳答案
您需要使用elif
,不是if
,在你的第二个 block 中:
if (y<h/3):
...
elif (y>(h*2/3)): #<-- change if to elif
...
else:
...
否则最终else
即使 y < h/3
也会被执行.
关于python - 程序无法识别 y 值 < 总高度的三分之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17511492/