jython JES:添加渐晕效果

0 投票
1 回答
787 浏览
提问于 2025-04-19 01:30

我正在尝试在JES中创建一个程序,里面有两个功能,一个是main(),另一个是addVignette(inputPic, vignette)。

main()这个功能是用来创建两个图片对象的。我想让用户选择一张输入图片(也就是要处理的那张图片),然后再让用户选择一个渐晕效果(vignette_profile.jpg)。一旦这两个图片对象创建完成,就应该调用addVignette(inputPic, vignette)这个功能。

addVignette(inputPic, vignette)这个功能需要接受两个图片对象作为参数。这两个图片对象是在main()函数中创建的,并作为输入传递给这个功能。简单来说,我需要用这两个图片对象,让我的功能对输入图片中的每一个像素进行渐晕效果的添加操作。编辑完成后的新图片应该显示在屏幕上。

我在把这两张图片相乘时遇到了问题。我不确定是代码有问题,还是我的公式不对。我不知道具体该怎么写代码,因为渐晕效果的边缘比较暗,而中心比较亮。

谢谢大家!

    def main():
    file1 = pickAFile()
    file2 = pickAFile()
    inputPic=makePicture(file1)
    vignette=makePicture(file2)
    addVignette(inputPic,vignette)

    def addVignette(inputPic,vignette):
    if getWidth(inputPic)==getWidth(vignette) and getHeight(inputPic)==getHeight(vignette):
    explore(inputPic)
    explore(vignette)
    allpx=getAllPixels(inputPic)
    for px in getAllPixels(inputPic):
    x=getX(px)
    y=getY(px)
    px2=getPixelAt(vignette,x,y)
    x1=getX(px)
    y2=getY(px)
    r1=getRed(px)
    r2=getRed(px2)
    g1=getGreen(px)
    g2=getGreen(px2)
    b1=getBlue(px)
    b2=getBlue(px2)



    if (1<r2<137): 
      r3=(r2-r1)-33
      g3=(g2-g1)+21
      b3=(b1-b2)+51

    if (138<r2<210):
      r3=(r2-r1)-21
      g3=(g2-g1)+49
      b3=(b1-b2)+121

    if (211<r2<246):
      r3=(r2-r1)+66
      g3=(g2-g1)+138
      b3=(b1-b2)+177

    if (247<r2<255):
      r3=(r2-r1)+44
      g3=(g2-g1)+125
      b3=(b2-b1)+201

    setRed(px,r3)
    setGreen(px,g3)
    setBlue(px,b)

  explore(inputPic)  
   else:
   print "Try Again"    

1 个回答

0

这可能不是你想要的,但我从你的问题中理解到,你有两张图片想要合成在一起,其中一张恰好是一个渐晕效果的图片。

请把你的 addVignette() 替换成下面的代码:

def addVignette(inputPic,vignette):
  # Cycle through each pixel in the input image
  for oPixel in getPixels(inputPic):

    # Get the pixel from the Vignette image that is at the same
    # location as the current pixel of the input image
    vignettePixel = getPixel(vignette, getX(oPixel), getY(oPixel))

    # Get the average of the color values by adding the color
    # values from the input & vignette together and then dividing
    # by 2
    newRed = (getRed(oPixel) + getRed(vignettePixel)) / 2
    newGreen = (getGreen(oPixel) + getGreen(vignettePixel)) / 2
    newBlue = (getBlue(oPixel) + getBlue(vignettePixel)) / 2

    # Make a new color from those values
    newColor = makeColor(newRed, newGreen, newBlue)

    # Assign this new color to the current pixel of the input image
    setColor(oPixel, newColor)

  explore(inputPic)

下面是我用测试图片得到的结果:

输入图片

渐晕效果

这里输入图片描述

撰写回答