合并两个图像,将RGB值乘以图像Jython / Python

时间:2014-09-06 14:18:33

标签: python jython jes

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图像也保存图像不工作,即使我一直试图让它工作任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

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

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

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

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

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

答案 1 :(得分:0)

保存图像

首先让我保存图像。我从您发布的代码中可以看到,您实际上从未调用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)