Jython JES:无法更改图像亮度

1 投票
1 回答
834 浏览
提问于 2025-04-18 18:41

有人能帮我一下吗?

我刚开始接触Jython/Python(其实是编程),现在正在使用一个叫JES的库,这个库让我可以很简单地修改图片等等。

我想通过两个输入来改变图片的亮度,一个是图片,另一个是亮度的数值。

def change(picture, amount):
  for px in getPixels(picture):
   color = getColor(px)
   alter = makeColor(color * amount)
   setColor(px, alter)

我试过很多其他的方法,但都不太管用。顺便说一下,图片的输入已经指定了一张图片。

我在终端里输入change(picture, 0.5)来运行程序,这应该会让图片亮50%,但我总是收到这个错误:

>>> change(picture, 0.5)
The error was: 'instance' and 'float'
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.

你们能帮我一下吗?谢谢!

1 个回答

1

试着把变量 color 打印到控制台上。你会在控制台上看到以下内容:

color r=255 g=255 b=255

这是因为方法 getColor(px) 返回了一个颜色对象。这个对象有三个属性 r、g、b,分别代表像素 px 的红色、绿色和蓝色的值。

现在你的问题是,方法 makeColor() 只接受一个 color 对象作为参数。此时你试图把一个 color 乘以 amount,但在乘法运算时你需要处理的是 数字 而不是颜色。

  def change(picture, amount):

    for px in getPixels(picture):
      # Get r,g,b values of the pixel and 
      myRed = getRed(px) / amount
      myBlue = getBlue(px) / amount
      myGreen = getGreen(px) / amount

      # use those values to make a new color
      newColor = makeColor(myRed, myGreen, myBlue)
      setColor(px, newColor)

撰写回答