Summer2006 Midterm 1 Review: Generalized changeColor
Questions?
This is what I tried and it does not work, says it is expecting an integer. I can't find in the book or lecture notes how to do this for more than one color at a time. Help?
def changeColor(picture):
changeRed(picture)
changeGreen(picture)
def changeRed(picture,amount):
for p in getPixels(picture):
value=getRed(p)
setRed=(p,value*amount)
def changeGreen(picture,amount):
for p in getPixels(picture):
value=getGreen(p)
setGreen=(p,value*amount)
| Your changeColor function should take in 3 parameters (read the question again). Then, check whether your third parameter is 'red', 'green' or 'blue', and then based on that call the appropriate functions like changeRed, changeGreen and changeBlue. - Bobby Mathew |
def changeColor(picture, amount, colorcode):
for p in getPixels(picture):
if (-.99<=amount<=.99):
if colorcode == red:
setRed(p,getRed(p)*(amount))
if colorcode == blue:
setBlue(p,getBlue(p)*(amount))
if colorcode == green:
setGreen(p,getGreen(p)(amount))
| red, green and blue should be within single quotes. Another really important thing is that the question asks you to increase or decrease it by a certain percentage, not just set it to the given percentage. So you have to adjust your calculations as well. - Bobby Mathew |
def changeColor(picture, amount, color):
amount=amount+1
for p in getPixels(picture):
if color == 'red':
r=getRed(p)
setRed(p,r*amount)
if color =='green':
g=getGreen(p)
setGreen(p,g*amount)
if color =='blue':
b=getBlue(p)
setBlue(p,b*amount)
| That's right. Good job! If anyone else needs an explanation, amount = amount + 1 makes the adjustment so that you are increasing or decreasing it by a certain percentage instead of just multiplying it straight away. - Bobby Mathew |
This is a post for the next question: Generalized Image Filtering. The ability to add to that page has been removed.
def changeRGB(picture, amountA, amountB, amountC):
amountA = amountA + 1
amountB = amountB + 1
amountC = amountC + 1
for p in getPixels(picture):
red = getRed(p)
greeen = getGreen(p)
blue = getBlue(p)
setRed(p, red*amountA)
setGreen(p, green*amountB)
setBlue(p, blue*amountC)
Link to this Page