JES中如何将图片的一部分做成灰度?
How to make a part of the picture grayscale in JES?
您好,我正在尝试使用我的代码,但似乎真的想不出如何将我的部分代码变成灰度图。这是我的代码:
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)
感谢任何帮助,真的很努力地理解这一点。再次感谢您的帮助!
显示错误:
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
这里有几件事给您带来麻烦:
- 在 y for 循环之后缩进您的代码(我猜您希望遍历所有高度)。
- 新颜色只是你当前像素的平均值,所以你需要先用加法相加再除以三。
- setColor() 接受一个像素和一个颜色对象。您要更改的像素是oldpixel,颜色对象是使用makeColor()创建的。
这是实现了所有修复的代码:
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)
我这样做了:
def greyTone():
#pict:Picture
pict=makePicture(pickAFile())
for p in getPixels(pict):
intensity=(getRed(p)+getGreen(p)+getBlue(p))*0.1
greyTone=makeColor(intensity+intensity+intensity)
setColor(p,greyTone)
show(pict)
我的教授使用了 r+g+b/3 变体,但是当我 运行 代码时,图片非常明亮,所以我发现了这个变体并且它工作得非常好,我希望这对您有所帮助: )
您好,我正在尝试使用我的代码,但似乎真的想不出如何将我的部分代码变成灰度图。这是我的代码:
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)
感谢任何帮助,真的很努力地理解这一点。再次感谢您的帮助!
显示错误:
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
这里有几件事给您带来麻烦:
- 在 y for 循环之后缩进您的代码(我猜您希望遍历所有高度)。
- 新颜色只是你当前像素的平均值,所以你需要先用加法相加再除以三。
- setColor() 接受一个像素和一个颜色对象。您要更改的像素是oldpixel,颜色对象是使用makeColor()创建的。
这是实现了所有修复的代码:
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)
我这样做了:
def greyTone():
#pict:Picture
pict=makePicture(pickAFile())
for p in getPixels(pict):
intensity=(getRed(p)+getGreen(p)+getBlue(p))*0.1
greyTone=makeColor(intensity+intensity+intensity)
setColor(p,greyTone)
show(pict)
我的教授使用了 r+g+b/3 变体,但是当我 运行 代码时,图片非常明亮,所以我发现了这个变体并且它工作得非常好,我希望这对您有所帮助: )