Tuesday, November 15, 2016

Image Portfolio

Image Portfolio

Rose Colored Glasses


















This function takes an image and adds a rosy hue to it. To achive this effect, I manipulated the green and blue values of each pixel. Each green value became 40% of the red value for that pixel and each blue value became 80% of the red value for that pixel. Here is the corresponding code:

def roseColoredGlasses(picture):
  pic = picture
  pixels = getPixels(pic)
  for p in pixels:
    r = getRed(p)
    setGreen(p, r * .4)
    setBlue(p, r * .8)
  repaint (pic)
  writePictureTo(pic, "D://Downloads//School//CST 205//Week 1 Pictures//Rose.jpg")

Negative


























This functiong takes an image and creates a negative image of it. To achieve, this effect I took the RGB values for each pixel and subtracted each value from 255 to get the negative RGB value. During this exercise, I learned that each RGB value ranges between 0 and 255. Here is the corresponding code:

def makeNegative(picture):
  pic = picture
  pixels = getPixels(pic)
  for p in pixels:
    r = getRed(p)
    r = 255 - r
    setRed(p, r)
    g = getGreen(p)
    g = 255 - g
    setGreen(p, g)
    b = getBlue(p)
    b = 255 - b
    setBlue(p, b)
  repaint (pic)
  writePictureTo(pic, "D://Downloads//School//CST 205//Week 1 Pictures//Negative.jpg")

Better Black and White


























This function takes a color image and converts it to a black and white image. To achieve this effect, I took the RGB values for each pixel and then weighted them so that R = 29.9%, G = 58.7%, and B = 11.4%. I then added these values up and divided by 3 to get a luminace value for the pixel. The luminace value is then used to set the RGB value for the pixel because all RGB values for a pixel must be equal to achieve a gray scale. Here is the corresponding code:

def betterBnW(picture):
  pic = picture
  pixels = getPixels(pic)
  for p in pixels:
    r = getRed(p)
    g = getGreen(p)
    b = getBlue(p)
    luminace = ((r * .299) + (g * .587) + (b * .114))
    setRed(p, luminace)
    setGreen(p, luminace)
    setBlue(p, luminace)
  repaint (pic)
  writePictureTo(pic, "D://Downloads//School//CST 205//Week 1 Pictures//BlackandWhite.jpg")

Bottom to Top Mirror





























This function takes an image and mirrors it horizontally by taking the bottom portion of the image and applying it upside down to the top portion of the image. The loop on the y axis starts at the center of the image and works it's way down to the bottom of the page. The loop on the on the x axis works left to right across the x axis. It then takes the pixel and copies it tothe pixel the middle of the page working its way up. Here is corresponding code:

def horizontalMirrorBottomTop(picture):
  pic = picture
  y2 = getHeight(pic)/2
  for y in range(getHeight(pic)/2, getHeight(pic)):
    y2 = y2 - 1
    for x in range(0, getWidth(pic)):
      p = getPixel(pic, x, y)
      p2 = getPixel(pic, x, y2)
      col = getColor(p)
      setColor(p2, col)
  repaint(pic)
  writePictureTo(pic, "D://Downloads//School//CST 205//Week 1 Pictures//horizontalMirrorBottomTop.jpg")

Shrink





















This function takes an image and creates a copy of the image that is half the size. This is done by omitting every other pixel in the image and applying it to the new canvas that is half the size of the original. Here is the corresponding code:

def shrink(picture):
  pic = picture
  mypic = makeEmptyPicture((getWidth(pic)/2) + 1, (getHeight(pic)/2) + 1)
  x2 = -1
  for x in range(0, getWidth(pic), 2):
    x2 = x2 + 1
    y2 = 0
    for y in range(0, getHeight(pic), 2):
      p = getPixel(pic, x, y)
      p2 = getPixel(mypic, x2, y2)
      col = getColor(p)
      setColor(p2, col)
      y2 = y2 + 1
  show(mypic)
  writePictureTo(pic, "D://Downloads//School//CST 205//Week 1 Pictures//shrinkBurger.jpg")
  return mypic

Collage

























This function utilized all I had learned about image manipulation in Python so far to create a collage of images. Several functions were used to create this collage. They include: makeCollage() function, Copy() function, pyCopy(), makeBlankPic(), verticalMirror(), horizontalMirror(), qaudrupleMirror(), rotatePic(), shrink(), roseColoredGlasses(), makeNegative(), artify(), and chromakey() functions. This project was not difficult just time consuming as much time was spent in the planning phase of getting the images put in the right spot on the new canvas. Here is the corresponding code:

# This function creates a collage of manipulated images on a 9.5 X 11 inch canvas
def makeCollage():
  background = makePicture("D://Downloads//School//CST 205//Week 1 Pictures//grey_background.gif")
  vmirror = makePicture("D://Downloads//School//CST 205//Week 1 Pictures//Star Wars Background.jpg")
  vmirror = verticalMirror(vmirror)
  hmirror = makePicture("D://Downloads//School//CST 205//Week 1 Pictures//Tortuga Hall.jpg")
  hmirror = horizontalMirror(hmirror)
  rotatemirror = rotatePic(hmirror)
  qmirror = makePicture("D://Downloads//School//CST 205//Week 1 Pictures//Y.jpg")
  qmirror = quadrupleMirror(qmirror)
  grouppic = makePicture("D://Downloads//School//CST 205//Week 1 Pictures//Group Photo 2.png")
  shrunk = makePicture("D://Downloads//School//CST 205//Week 1 Pictures//O.jpg")
  shrunk = shrink(shrunk)
  tpic = makePicture("D://Downloads//School//CST 205//Week 1 Pictures//T.jpg")
  tpic = roseColoredGlasses(tpic)
  negative = makePicture("D://Downloads//School//CST 205//Week 1 Pictures//Earth.jpg")
  negative = makeNegative(negative)
  otter = makePicture("D://Downloads//School//CST 205//Week 1 Pictures//otter.jpg")
  otter = artify(otter)
  falcon = makePicture("D://Downloads//School//CST 205//Week 1 Pictures//Falcon.jpg")
  mountain = makePicture("D://Downloads//School//CST 205//Week 1 Pictures//Mountain.jpg")
  greenscreen = chromakey(falcon, mountain)
  blank = makeBlankPic()
  pic1 = pyCopy(background, blank, 0, 0)
  pic1 = pyCopy(vmirror, pic1, 33, 33)
  pic1 = pyCopy(rotatemirror, pic1, 2000, 33)
  pic1 = pyCopy(grouppic, pic1, 1600, 1000)
  pic1 = pyCopy(qmirror, pic1, 1250, 125)
  pic1 = pyCopy(shrunk, pic1, 143, 2033)
  pic1 = pyCopy(tpic, pic1, 33, 800)
  pic1 = pyCopy(negative, pic1, 1250, 1750)
  pic1 = pyCopy(greenscreen, pic1, 550, 2500)
  pic1 = pyCopy(otter, pic1, 1700, 2400)
  writePictureTo(pic1, "D://Downloads//School//CST 205//Week 1 Pictures//Collage.jpg")
  explore(pic1) 

# This function creates a simple copy of an image in  a larger background
def Copy():
  pic = get_pic()
  mypic = makeEmptyPicture(getWidth(pic)*2, getHeight(pic)*2)
  x2 = (getWidth(mypic)/4) - 1
  for x in range(0, getWidth(pic) - 1):
    y2 = getHeight(mypic)/4
    x2 = x2 + 1
    for y in range(0, getHeight(pic) - 1):
      p = getPixel(pic, x, y)
      p2 = getPixel(mypic, x2, y2)
      col = getColor(p)
      setColor(p2, col)
      y2 = y2 + 1
  show(mypic)
  return mypic
  
# This function creates a simple copy of an image in a larger background with given parameters
def pyCopy(source, target, targetX, targetY):
  oldpic = duplicatePicture(source)
  newpic = duplicatePicture(target)
  x2 = targetX
  for x in range(0, getWidth(oldpic) - 1):
    y2 = targetY
    x2 = x2 + 1
    for y in range(0, getHeight(oldpic) - 1):
      p = getPixel(oldpic, x, y)
      pixeltarget = getPixelAt(newpic, x2, y2)
      newcolor = getColor(p)
      setColor(pixeltarget, newcolor)
      y2 = y2 + 1
  return newpic
  
# This function makes a blank image of a sheet 8.5 x 11 inches
def makeBlankPic():
  return makeEmptyPicture(2550, 3300)
  
# This function creates a mirror image of a picture vertically down the middle
def verticalMirror(picture):
  pic = picture
  x2 = getWidth(pic)
  for x in range(0, getWidth(pic)/2):
    x2 = x2 - 1 
    for y in range(0, getHeight(pic)):
      p = getPixel(pic, x, y)
      p2 = getPixel(pic, x2, y) 
      col =  getColor(p)
      setColor(p2, col)
  return(pic)
  
# This function creates a mirror image of a picture horizontally across the middle
def horizontalMirror(picture):
  pic = picture
  y2 = getHeight(pic)
  for y in range(0, getHeight(pic)/2):
    y2 = y2 - 1
    for x in range(0, getWidth(pic)):
      p = getPixel(pic, x, y)
      p2 = getPixel(pic, x, y2)
      col = getColor(p)
      setColor(p2, col)
  return pic
  
# This function creates a quadruple mirror of an impage
def quadrupleMirror(picture):
  pic = picture
  x2 = getWidth(pic)
  for x in range(0, getWidth(pic)/2):
    x2 = x2 - 1 
    for y in range(0, getHeight(pic)):
      p = getPixel(pic, x, y)
      p2 = getPixel(pic, x2, y) 
      col =  getColor(p)
      setColor(p2, col)
  y2 = getHeight(pic)
  for y in range(0, getHeight(pic)/2):
    y2 = y2 - 1
    for x in range(0, getWidth(pic)):
      p = getPixel(pic, x, y)
      p2 = getPixel(pic, x, y2)
      col = getColor(p)
      setColor(p2, col)
  return pic
 
  
# This function rotates a picture 90 degrees and creates a copy of it
def rotatePic(picture):
  pic = picture
  mypic = makeEmptyPicture(getHeight(pic), getWidth(pic))
  for x in range(0, getWidth(pic)):
    x2 = getWidth(mypic)
    for y in range(0, getHeight(pic)):
      x2 = x2 - 1
      p = getPixel(pic, x, y)
      p2 = getPixel(mypic, x2, x)
      col = getColor(p)
      setColor(p2, col)
  return mypic
  
  
# This function creates a copy of a picture that is half the size
def shrink(picture):
  pic = picture
  mypic = makeEmptyPicture((getWidth(pic)/2) + 1, (getHeight(pic)/2) + 1)
  x2 = -1
  for x in range(0, getWidth(pic), 2):
    x2 = x2 + 1
    y2 = 0
    for y in range(0, getHeight(pic), 2):
      p = getPixel(pic, x, y)
      p2 = getPixel(mypic, x2, y2)
      col = getColor(p)
      setColor(p2, col)
      y2 = y2 + 1
  return mypic

# This function makes the picture have a pink hue
def roseColoredGlasses(picture):
  pic = picture
  pixels = getPixels(pic)
  for p in pixels:
    r = getRed(p)
    setGreen(p, r * .4)
    setBlue(p, r * .8)
  return pic
   
# This function makes a negative image of the picture
def makeNegative(picture):
  pic = picture
  pixels = getPixels(pic)
  for p in pixels:
    r = getRed(p)
    r = 255 - r
    setRed(p, r)
    g = getGreen(p)
    g = 255 - g
    setGreen(p, g)
    b = getBlue(p)
    b = 255 - b
    setBlue(p, b)
  return pic
      
# This function creates a customized posterization of an image
def artify(picture):
  pic = picture
  pixels = getPixels(pic)
  for p in pixels:
    r = getRed(p)
    g = getGreen(p)
    b = getBlue(p)
    if r < 33:
      setRed(p, 33)
    elif 32 < r < 99:
      setRed(p, 66)
    elif 98 < r < 133:
      setRed(p, 99)
    elif 132 < r < 177:
      setRed(p, 133)
    elif 176 < r < 199:
      setRed(p, 177)
    elif 198 < r < 233:
      setRed(p, 199)
    elif 232 < r < 256:
      setRed(p, 233)
    if g < 33:
      setGreen(p, 33)
    elif 32 < g < 99:
      setGreen(p, 66)
    elif 98 < g < 133:
      setGreen(p, 99)
    elif 132 < g < 177:
      setGreen(p, 133)
    elif 176 < g < 199:
      setGreen(p, 177)
    elif 198 < g < 233:
      setGreen(p, 199)
    elif 232 < g < 256:
      setGreen(p, 233)
    if b < 33:
      setBlue(p, 33)
    elif 32 < b < 99:
      setBlue(p, 66)
    elif 98 < b < 133:
      setBlue(p, 99)
    elif 132 < b < 177:
      setBlue(p, 133)
    elif 176 < b < 199:
      setBlue(p, 177)
    elif 198 < b < 233:
      setBlue(p, 199)
    elif 232 < b < 256:
      setBlue(p, 233)
  return pic
  
def chromakey(picture1, picture2):
  pica = picture1   # Green Screen
  picb = picture2   # Background
  pixels = getPixels(pica)
  pixelsB = getPixels(picb)
  for p in pixels:
     r = getRed(p)
     g = getGreen(p)
     b = getBlue(p)
     if (r + b) <= g:
       xA = getX(p)
       yA = getY(p)
       pixB = getPixel(picb, xA, yA)
       newColor = getColor(pixB)
       setColor(p, newColor)
  return pica

Red Eye Reduction























f





















This function takes an image and reduces the red eye in the image. Being one of the first functions to create, it was a bit challenging as it was not understood yet about reducing the red in a given section of an image. Instead applied the red eye reduction to the entire image which is why there is black pixelation in the bottom right portion of the image. Here is the corresponding code:

def redEye(picture):
  picture = picture
  redRem = makeColor(235,115,132)
  orang = makeColor(521,227,107)
  orang1 = makeColor(184, 131, 45)
  yelo = makeColor(253,249,211)
  aqua = makeColor(51,204,255)
  red1 = makeColor(155, 42, 45)
  red2 = makeColor(198, 79, 86)
  for x in range(0,getWidth(picture)):
    for y in range(0,getHeight(picture)):
      px = getPixel(picture,x,y)
      color = getColor(px)
      if distance(color, redRem) < 50.0:
        setColor(px,black) 
      if distance(color,yelo) < 50.0:
        setColor(px,black) 
      if distance(color, orang) < 80.0:
        setColor(px,black)
      if distance(color, orang1) < 50.0:
        setColor(px, black)
      if distance(color, red1) < 50.0:
        setColor(px, black)
      if distance(color, red2) < 50.0:
        setColor(px, black)
  show(picture)
  writePictureTo(picture, "D://Downloads//School//CST 205//Week 1 Pictures//redEyeRabbit.jpg")
  return(picture)

Color Artify

































This function creates a customized posterization of an image. It utilizeds if  and elif statements to determine a RGB value range and if it falls within a certan range sets the value accordingly. Here is the corresponding code:

def artify(picture):
  pic = picture
  pixels = getPixels(pic)
  for p in pixels:
    r = getRed(p)
    g = getGreen(p)
    b = getBlue(p)
    if r < 33:
      setRed(p, 33)
    elif 32 < r < 99:
      setRed(p, 66)
    elif 98 < r < 133:
      setRed(p, 99)
    elif 132 < r < 177:
      setRed(p, 133)
    elif 176 < r < 199:
      setRed(p, 177)
    elif 198 < r < 233:
      setRed(p, 199)
    elif 232 < r < 256:
      setRed(p, 233)
    if g < 33:
      setGreen(p, 33)
    elif 32 < g < 99:
      setGreen(p, 66)
    elif 98 < g < 133:
      setGreen(p, 99)
    elif 132 < g < 177:
      setGreen(p, 133)
    elif 176 < g < 199:
      setGreen(p, 177)
    elif 198 < g < 233:
      setGreen(p, 199)
    elif 232 < g < 256:
      setGreen(p, 233)
    if b < 33:
      setBlue(p, 33)
    elif 32 < b < 99:
      setBlue(p, 66)
    elif 98 < b < 133:
      setBlue(p, 99)
    elif 132 < b < 177:
      setBlue(p, 133)
    elif 176 < b < 199:
      setBlue(p, 177)
    elif 198 < b < 233:
      setBlue(p, 199)
    elif 232 < b < 256:
      setBlue(p, 233)
  repaint(pic)
  writePictureTo(pic, "D://Downloads//School//CST 205//Week 1 Pictures//Artify.jpg")

Green Screen











































This function takes a green screen image and another image and replaces the green screen portion with the second image. To achieve this an if statement is used to determine if the red plus blue value of a pixel are less than or equal to the green value of the pixel. If it is it replaces that pixel which should be the green screen portion with the pixel from the other image. Here is the corresponding code:

def chromakey(picture1, picture2):
  pica = picture1   # Green Screen
  picb = picture2   # Background
  pixels = getPixels(pica)
  pixelsB = getPixels(picb)
  for p in pixels:
     r = getRed(p)
     g = getGreen(p)
     b = getBlue(p)
     if (r + b) <= g:
       xA = getX(p)
       yA = getY(p)
       pixB = getPixel(picb, xA, yA)
       newColor = getColor(pixB)
       setColor(p, newColor)
  show(pica)
  writePictureTo(pica, "D://Downloads//School//CST 205//Week 1 Pictures//Chromakeyexample.jpg")

Thanksgiving Card




















































This project utilized several functions to merge an image onto a green screen image and copy the turkey image on top. The turkey image had to go through the makeNegative() function so the black background merged with the main canvas black background. It then used a function to create an orange hue to align with Fall colors. Text was added in a for loop to get it to appear on seperately lines. This was a challence as it was discovered that the addText() function in JES does not accurately utilize \n to go to a next line in a string passed to it. Here is the corresponding code:

#This function creates a Star Wars themed Thanksgiving Card
def thanksgivingCard():
  import java.awt.Font as Font                   #Import font
  card = chromakey()                             #Basis for greeting card
  turkey = makePicture("D://Downloads//School//CST 205//Week 1 Pictures//turkey1.jpg")
  turkey = makeNegative(turkey)                  #Make negative image of turkey picture to blend in
  card = pyCopy(turkey, card, 324, 437)          #Apply turkey to card
  str = ['This Thanksgiving,', 'Choose Between', 'The Dark Side', 'or the Light', 'Side of the', 'Turkey!', 'Happy Thanksgiving!']
  myFont = makeStyle("Broadway", Font.BOLD, 34)
  myFont2 = makeStyle("Broadway", Font.BOLD, 42)
  y = 34                                         #Initial y value for text
  x = 0                                          #Horizontal x value
  for t in range(0, 6):                          #Loop for adding text
    addTextWithStyle(card, 424 + x, y, str[t], myFont, white)
    y = y + 34
    x = x + 15
  addTextWithStyle(card, 750, 204, str[6], myFont2, white)
  card = makeOrange(card)                        #Create an orange hue effect
  show(card)
  writePictureTo(card, "D://Downloads//School//CST 205//Week 1 Pictures//GreetingCard.jpg")

#This function takes a green screen image and applies a background to it
def chromakey():
  pica = makePicture("D://Downloads//School//CST 205//Week 1 Pictures//falcon1.jpg")  #Green Screen
  picb = makePicture("D://Downloads//School//CST 205//Week 1 Pictures//darthmonochrome.jpg")   # Background
  pixels = getPixels(pica)
  pixelsB = getPixels(picb)
  for p in pixels:
     r = getRed(p)
     g = getGreen(p)
     b = getBlue(p)
     if (r + b) <= g:
       xA = getX(p)
       yA = getY(p)
       pixB = getPixel(picb, xA, yA)
       newColor = getColor(pixB)
       setColor(p, newColor)
  return(pica)
  
# This function makes the picture have an orange hue
def makeOrange(picture):
  pic = picture
  pixels = getPixels(pic)
  for p in pixels:
    r = getRed(p)
    setGreen(p, r * .65)
    setBlue(p, 0)
  repaint (pic)
  return pic
  
# This function creates a simple copy of an image in a larger background with given parameters
def pyCopy(source, target, targetX, targetY):
  oldpic = duplicatePicture(source)
  newpic = duplicatePicture(target)
  x2 = targetX
  for x in range(0, getWidth(oldpic) - 1):
    y2 = targetY
    x2 = x2 + 1
    for y in range(0, getHeight(oldpic) - 1):
      p = getPixel(oldpic, x, y)
      pixeltarget = getPixelAt(newpic, x2, y2)
      newcolor = getColor(p)
      setColor(pixeltarget, newcolor)
      y2 = y2 + 1
  return newpic
  
# This function makes a negative image of the picture
def makeNegative(picture):
  pic = picture
  pixels = getPixels(pic)
  for p in pixels:
    r = getRed(p)
    r = 255 - r
    setRed(p, r)
    g = getGreen(p)
    g = 255 - g
    setGreen(p, g)
    b = getBlue(p)
    b = 255 - b
    setBlue(p, b)
  return pic

Line Drawing

































This function creates a black and white drawing like image of a given picture. To achieve this first the image is turned into a black and white image. Then it runs through each pixel and creates a luminaance function using lambda. The luminance is the average of the RGB value for that pixel. It then calculates the luminance to the right of the pixe, below the pixel and for the pixel itself. If the absolute value of the luminance between the right pixel and below pixel is  > 4 and the absolute value of the luminance between the pixel below and the original pixel is  > 4, then its sets the color to black. Otherwise the color gets set to white creating a sketch pencil drawing effect. This function was easy enough to create. I just had to toy with a correct absolute value to achieve the desired effect. Here is the corresponding code:

def lineDwg(pic):
  #turn B&W
  pic = betterBnW(pic)
  picture = duplicatePicture(pic)
  width, height = getWidth(pic) , getHeight(pic)

  for y in xrange(height - 1):
    for x in xrange(width - 1):
    
      px = getPixel(picture, x, y) # original pixel
      right = getPixel(picture, x + 1, y)#right pixel
      below = getPixel(picture, x, y + 1)#below pixel
    
      #get luminance
      luminance = lambda px: (getRed(px) + getBlue(px) + getGreen(px) )/3
    
      LR = luminance(right)#luminance to right pizel
      LB = luminance(below)#luminance below pixel
      LO = luminance(px)#luminance of original pixel
      #pixel edge
      if abs(LR - LO) > 4 and abs(LB - LO) > 4:
        #return b&w image
        setColor(px, black)
      else:
        setColor(px, white)

  show(picture)
  writePictureTo(picture, "D://Downloads//School//CST 205//Week 1 Pictures//lineDwg.jpg")
  
# This function makes a more suitable black and white picture
def betterBnW(picture):
  pic = picture
  pixels = getPixels(pic)
  for p in pixels:
    r = getRed(p)
    g = getGreen(p)
    b = getBlue(p)
    luminace = ((r * .299) + (g * .587) + (b * .114))
    setRed(p, luminace)
    setGreen(p, luminace)
    setBlue(p, luminace)
  return (pic)
  

No comments:

Post a Comment