组合两个图像,用Jython / Python将RGB值相乘以生成图像的暗角

2024-10-01 15:29:10 发布

您现在位置:Python中文网/ 问答频道 /正文

http://i.stack.imgur.com/AAtUD.jpghttp://i.stack.imgur.com/eouLY.jpg


用于代码的图像。在


我要做的最终结果是合并渐晕图片和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_配置文件图像,然后使用CGI图像,保存的图像也不起作用,即使我一直在尝试使其工作,任何帮助都将是感激的。在


Tags: 图像httpstackdef图片边缘jpgcanvas
2条回答

你也可以尝试在简历中这样做。单像素操作和文件I/O非常直接。在

img = cv2.imread('test.jpg')
pixel = img[10,10]

我从来没有任何问题的文件I/O在简历。很有可能是权限错误或空格过多。在

^{pr2}$

你也可以做一些简单的图像预览,在这种情况下,可以让你的实验输出多一点。在

保存图像

让我从保存图像开始。从您发布的代码中我可以看到,您从来没有真正调用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)

相关问题 更多 >

    热门问题