Python 如何在JES中制作部分图像灰度?

Python 如何在JES中制作部分图像灰度?,python,image,jython,partial,Python,Image,Jython,Partial,大家好,我正试着玩弄我的代码,但似乎真的不知道如何将部分代码转换成灰度。这是我的代码: 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)

大家好,我正试着玩弄我的代码,但似乎真的不知道如何将部分代码转换成灰度。这是我的代码:

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)
我们非常感谢您的帮助,非常努力地理解这一点。再次感谢你的帮助

错误显示:

灰度(nP) 错误是:“tuple”和“int”

参数类型不正确。 试图使用无效类型的参数调用函数。这意味着您做了一些事情,例如尝试将字符串传递给需要整数的方法。 请检查/Users/enochpan/Desktop/test的第8行


有几件事会给你带来麻烦:

  • 代码在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)