1

https://i.stack.imgur.com/AAtUD.jpg https://i.stack.imgur.com/eouLY.jpg


images to use for code.


the end result i am trying to do is combine the vignette picture and the CGI picture because the vignette images RGB values are darker towards the edges i need to multiply the original images corresponding pixels by the smaller numbers towards the edges, should make the picture have a darker frame around the edges of the original picture.

here's the code so far:

  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) 

when testing use vignette_profile image first then CGI image also the saving image doesnt work even though i've been trying to get it to work any help will be appreciated.

Master_Cdawgg
  • 15
  • 1
  • 6

2 Answers2

0

You can also try doing this in CV. Single pixel manipulation and file I/O are pretty straight forward.

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

Ive never had any issues with file I/O in CV. Chances are its a permission error or excess white space.

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

You can also do some easy image previewing which in this case would let you experiment with the output a little more.

Meozaa
  • 95
  • 9
0

Saving the image

Let me start with saving the image. What I can see from the code you posted, you never actually call the newPicture() function which is why it's not saving the image. Also I noticed in the newPicture function that you don't pass a reference of the new image to the function.

Please my solution to this below. I have changed the function name from newPicture to saveNewImage()


Adding the Vignette

Please see the comments for the code block denote by ******* in the getNewColorValues() function.


You run the Main() function for this script to work

# 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)
BenMorel
  • 34,448
  • 50
  • 182
  • 322
super_mario3d
  • 95
  • 1
  • 5