Issue
Hi there I'm trying to play around with my code and can't really seem to figure out how to make part of my code into a grayscale. This is my code:
def grayScale(picture):
xstop=getWidth(picture)/2
ystop=getHeight(picture)/2
for x in range(0,xstop):
for y in range(0,ystop):
oldpixel= getPixel(picture,x,y)
colour=getColor(oldpixel)
newColor=(getRed(oldpixel),getGreen(oldpixel),getBlue(oldpixel))/3
setColor(picture,(newColor,newColor,newColor))
repaint(picture)
nP=makePicture(pickAFile())
show(nP)
Any help is appreciated, really trying hard to understand this. Thanks again for your help!
Error shown:
grayScale(nP) The error was: 'tuple' and 'int'
Inappropriate argument type. An attempt was made to call a function with a parameter of an invalid type. This means that you did something such as trying to pass a string to a method that is expecting an integer. Please check line 8 of /Users/enochphan/Desktop/test
Solution
There are a few things giving you trouble here:
- Indenting of your code after the y for loop (I'm guessing you want all of the height to traversed).
- The new colour is just the average of your current pixels, so you need to use addition to add them before dividing by three.
- setColor() takes a pixel and a color object. The pixel you want to change is oldpixel, and the color object is created using makeColor().
Here is code with all the fixes implemented:
def grayScale(picture):
xstop=getWidth(picture)/2
ystop=getHeight(picture)/2
for x in range(0,xstop):
for y in range(0,ystop):
oldpixel= getPixel(picture,x,y)
colour=getColor(oldpixel)
newColor = (getRed(oldpixel)+getGreen(oldpixel)+getBlue(oldpixel))/3
setColor(oldpixel,makeColor(newColor,newColor,newColor))
repaint(picture)
Answered By - Vaderico Answer Checked By - Gilberto Lyons (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.