【问题标题】:combining two images, multiplying RGB values to vignette an image Jython/Python组合两个图像,将 RGB 值相乘以对图像进行暗角 Jython/Python
【发布时间】:2014-09-07 15:09:57
【问题描述】:

http://i.stack.imgur.com/AAtUD.jpg http://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_profile 图像,然后是 CGI 图像,保存图像也不起作用,即使我一直试图让它工作,任何帮助都将不胜感激。

【问题讨论】:

标签: python jython jes


【解决方案1】:

保存图片

让我从保存图像开始。我从您发布的代码中可以看到,您实际上从未调用过 newPicture() 函数,这就是它不保存图像的原因。我还注意到在 newPicture 函数中,您没有将新图像的引用传递给函数。

请在下面解决我的问题。我已将函数名称从 newPicture 更改为 saveNewImage()


添加小插图

请查看getNewColorValues()函数中*******表示的代码块的cmets。


您运行 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)

【讨论】:

    【解决方案2】:

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

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

    我在 CV 中的文件 I/O 从未遇到过任何问题。可能是权限错误或多余的空白。

    cv2.imwrite('messigray.png',img)
    

    您还可以进行一些简单的图像预览,在这种情况下,您可以对输出进行更多试验。

    【讨论】:

    • 感谢您提供使小插图正常工作的替代解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多