组合两个图像,将RGB值相乘以形成一个图像Jython/Python

组合两个图像,将RGB值相乘以形成一个图像Jython/Python,python,jython,jes,Python,Jython,Jes,用于代码的图像 我试图做的最终结果是结合渐晕图和CGI图,因为渐晕图图像RGB值在边缘处较暗,我需要将原始图像对应的像素乘以边缘处较小的数字,应该使图像在原始图像边缘周围有一个较暗的帧 以下是迄今为止的代码: def addVignette(inputPic, vignette): #create empty canvas to combine images correctly canvas = makeEmptyPicture(getWidth(inputPic), getHe

用于代码的图像

我试图做的最终结果是结合渐晕图和CGI图,因为渐晕图图像RGB值在边缘处较暗,我需要将原始图像对应的像素乘以边缘处较小的数字,应该使图像在原始图像边缘周围有一个较暗的帧

以下是迄今为止的代码:

  def addVignette(inputPic, vignette):
   #create empty canvas to combine images correctly
   canvas = makeEmptyPicture(getWidth(inputPic), getHeight(inputPic))

   for x in range(0, getWidth(inputPic)):
     for y in range(0, getHeight(inputPic)):
       px = getPixel(canvas, x, y)
       inputPx = getPixel(inputPic, x, y)
       vignettePx = getPixel(vignette, x, y)

      #make a new color from these values
       newColour = getNewColorValues(vignettePx,inputPx)

      #then assign this new color to the current pixel of the input image
       setColor(px, newColour)

  explore(canvas)

def getNewColourValues(inputPx, vignettePx):

   inputRed = getRed(inputPx)
   vignetteRed = getRed(vignettePx)
   inputGreen = getGreen(inputPx)
   vignetteGreen = getGreen(vignettePx)
   inputBlue = getBlue(inputPx)
   vignetteBlue = getBlue(vignettePx)


   newRGB= setColor(inputPx,inputRed,inputGreen,inputBlue)*(vignettePx,vignetteRed,vignetteGreen,vignetteBlue)


   newColour = makeColor(newRGB) 

   return newColour

def newPicture(newColour):

 folder = pickAFolder()
 filename = requestString("enter file name: ")
 path = folder+filename+".jpg"

 writePictureTo(inputPic, path) 

测试时,请先使用vignette\u配置文件图像,然后使用CGI图像。即使我一直在尝试使其工作,保存图像也无法工作。我们将非常感谢您的帮助。

您也可以尝试在CV中执行此操作。单像素操作和文件I/O非常简单

img = cv2.imread('test.jpg')
pixel = img[10,10]
我在简历中从未遇到过文件I/O问题。很可能是权限错误或空白过多

cv2.imwrite('messigray.png',img)
您还可以进行一些简单的图像预览,在这种情况下,可以让您对输出进行更多的实验

保存图像 让我从保存图像开始。从你发布的代码中我可以看到,你从来没有调用newPicture函数,这就是为什么它没有保存图像。我还注意到在newPicture函数中,没有将新图像的引用传递给函数

请在下面给出我的解决方案。我已将函数名从newPicture更改为saveNewImage

添加小插曲 请参阅getNewColorValues函数中由*******表示的代码块的注释

运行Main函数使该脚本正常工作
我试图得到的结果与此类似:感谢您提供了另一种解决方案,让小插曲正常工作
# Main function.
# *** THIS FUNCTION NEEDS TO BE CALLED IN THE CONSOLE ***
# i.e >>> main()

def main():

  # Choose the files you wish to use
  inputFile = pickAFile()
  vignetteFile = pickAFile()

  # Turn both files into picture objects
  inputPic = makePicture(inputFile)
  vignette = makePicture(vignetteFile)

  # addVignette() function combines the input picture and vignette together
  # and returns the result as a new picture object
  newImage =  addVignette(inputPic, vignette)

  # saveNewImage() function stores the new image as file
  saveNewImage(newImage)


# Main() calls this function to add input picture and vignette together  
def addVignette(inputPic, vignette):

  # Create empty canvas
  canvas = makeEmptyPicture(getWidth(inputPic), getHeight(inputPic))

  # Iterate through all the pixels of the input image. x and y are
  # used as the current coordinates of the pixel
  for x in range(0, getWidth(inputPic)):
    for y in range(0, getHeight(inputPic)):

      # Get the current pixels of inputPic and vignette
      inputPixel = getPixel(inputPic, x, y)
      vignettePixel = getPixel(vignette, x, y)

      # The getNewColorValues() function, makes a new color from those
      # values
      newColor = getNewColorValues(inputPixel, vignettePixel)

      # Assign this new color to the current pixel of the canvas
      px = getPixel(canvas, x, y)      
      setColor(px, newColor)

  # Show the result of combiming the input picture with the vignette
  explore(canvas)

  # return the new image to main() function.
  return canvas

# Called from the addVignette() function to add the color values from
# the input picture and vignette together. It returns a new color
# object
def getNewColorValues(inputPixel, vignettePixel):

  # Get the individual colour values
  inputRed = getRed(inputPixel)
  vignetteRed = getRed(vignettePixel)
  inputGreen = getGreen(inputPixel)
  vignetteGreen = getGreen(vignettePixel)
  inputBlue = getBlue(inputPixel)
  vignetteBlue = getBlue(vignettePixel)

  # ***********************************************************
  # Most important part. This will determine if the pixel is darkent
  # and by how much. How it works is the darker the vignette pixel the less that will
  # be taken away from 255. This means the result of `255 - vignetteRed` will be a higher
  # value which means more will be taken away from the input colour.
  # The light the vignette pixel the less that will be taken away from input pixel

  newR = inputRed - (255 - vignetteRed)
  newG = inputGreen - (255 - vignetteGreen)
  newB = inputBlue - (255 - vignetteBlue)
  # ***********************************************************

  newC = makeColor(newR, newG, newB)
  return newC

# Called from the main() function in order to save the new image  
def saveNewImage(newImage):

  folder = pickAFolder()
  filename = requestString("Please enter file name: ")
  path = folder + filename + ".jpg"

  writePictureTo(newImage, path)