Difference between revisions of "CSC111 Lab 7 Solutions 2014"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- =Solution Programs/Functions= <onlydft> <source lang="python"> # fog( image ): creates a foggy image with the top line original to the # original line of pixels,...")
 
Line 3: Line 3:
 
=Solution Programs/Functions=
 
=Solution Programs/Functions=
  
<onlydft>
+
 
<source lang="python">
 
<source lang="python">
  
Line 182: Line 182:
  
 
</source>
 
</source>
</onlydft>
+
  
  

Revision as of 16:17, 13 March 2014

--D. Thiebaut (talk) 17:40, 10 March 2014 (EDT)


Solution Programs/Functions

# fog( image ): creates a foggy image with the top line original to the
# original line of pixels, but the bottom line is totally white, and a linear
# gradient in between full color and full white.
def fog( image ):
  for x in range(0,getWidth(image)):
    repaint( image )
    for y in range(0,getHeight(image)):
        pixel = getPixel (image, x, y)
        red   = getRed(pixel)
        green = getGreen(pixel)
        blue  = getBlue(pixel)

        red   = red + int( (255-red) * 1.0 * y/getHeight( image ) )
        green = green + int( (255-green) * 1.0 * y/getHeight( image ) )
        blue  = blue + int( (255-blue) * 1.0 * y/getHeight( image ) )

        newColor = makeColor( red, green, blue )
        setColor( pixel, newColor )
        
  return image  
      
# addRedSquare(): puts a red square with top-left corner (109, 117) and
# bottom-right corner (136, 139) 
def addRedSquare( image ):
  for x in range( 109, 136 ):
    for y in range(117, 139):
        pixel = getPixel (image, x, y)
        newColor = makeColor( 255, 0, 0 )
        setColor( pixel, newColor )

  return image
          
# mirrorLeft( image ): takes the left side of the image and mirrors it to the right
def mirrorLeft( image ):
  for x in range(0,getWidth(image)/2):
    repaint( image )
    for y in range(0,getHeight(image)):
        pixel = getPixel (image, x, y)
        red   = getRed(pixel)
        green = getGreen(pixel)
        blue  = getBlue(pixel)  
        newColor = makeColor( red, green, blue )
        setColor( pixel, newColor )
        pixel = getPixel( image, getWidth(image)-x-1, y )
        setColor( pixel, newColor )
        
  return image

# sweep( image ): demo showing the progress of the sweep transforming the pixels
# through the image.
def sweep( image ):
  for x in range(0,getWidth(image)):
    repaint( image )
    for y in range(0,getHeight(image)):
        pixel = getPixel (image, x, y)
        red   = getRed(pixel)
        green = getGreen(pixel)
        blue  = getBlue(pixel)
  
        # the line below replaces the pixel with its original color.  Change
        # the amount of red, green and blue to see some change in the colors
        if red < 125:
           red = green = blue = 0
        else:
           red = 255
        newColor = makeColor( red, green, blue )
        setColor( pixel, newColor )
  return image

# AndyWarhol( image ): saturates the image by saturating the 
# RGB components.  If the R component is less than 125, then
# set it to 0, otherwise set it to 255.   Same for blue and green.
def AndyWarhol( image ):
  for x in range(0,getWidth(image)):
    repaint( image )
    for y in range(0,getHeight(image)):
        pixel = getPixel (image, x, y)
        red   = getRed(pixel)
        green = getGreen(pixel)
        blue  = getBlue(pixel)
  
        # the line below replaces the pixel with its original color.  Change
        # the amount of red, green and blue to see some change in the colors
        if red < 125:
           red = 0
        else:
           red = 255
        if green < 125:
           green = 0
        else:
           green = 255
        if blue < 125:
           blue = 0
        else:
           blue = 255
        newColor = makeColor( red, green, blue )
        setColor( pixel, newColor )
  return image

# makeBlackAndWhite( image ).
# Changes the amount of red, green and blue that is in an image to make it grey
def makeBlackAndWhite( image ):
  for x in range(0,getWidth(image)):
    for y in range(0,getHeight(image)):
        pixel = getPixel (image, x, y)
        red   = getRed(pixel)
        green = getGreen(pixel)
        blue  = getBlue(pixel)

        #simplest way to create grey
        #grey  = (red + green + blue) //3 

        # best way to create grey
        grey  = int( 0.3 * red +0.6 * green +0.11 * blue )

        newColor = makeColor(grey, grey, grey )
        setColor( pixel, newColor )
  return image

# addBorder( image, borderWidth )
# adds a border around the image.  BorderWidth defines the width of the
# border, in pixels.
def addBorder( image, borderWidth ):
  for y in range( 0, borderWidth ):
    for x in range( 0, getWidth(image) ):
       pixel = getPixel( image, x, y )
       newColor = makeColor( 102, 0, 255 ) # purple
       setColor( pixel, newColor )

  for y in range( getHeight(image)-borderWidth, getHeight( image) ):
    for x in range( 0, getWidth(image) ):
       pixel = getPixel( image, x, y )
       newColor = makeColor( 102, 0, 255 ) # purple
       setColor( pixel, newColor )

  for y in range( 0, getHeight( image ) ):
    for x in range( getWidth( image )-borderWidth, getWidth(image) ):
       pixel = getPixel( image, x, y )
       newColor = makeColor( 102, 0, 255 ) # purple
       setColor( pixel, newColor )

  for y in range( 0, getHeight( image) ):
    for x in range( 0, borderWidth ):
       pixel = getPixel( image, x, y )
       newColor = makeColor( 102, 0, 255 ) # purple
       setColor( pixel, newColor )

  return image

# given the coordinates of the top-left, bottom-right points defining
# two rectangles around the eyes, find very red pixels and make their
# red component 0.
def fixRedEyes( image, x1, y1, x2, y2, x3, y3, x4, y4 ):
   for x in range( x1, x2 ):
      for y in range( y1, y2 ):
         pixel = getPixel (image, x, y)
         red   = getRed(pixel)
         green = getGreen(pixel)
         blue  = getBlue(pixel)
         newColor = makeColor( 0, green, blue )
         setColor( pixel, newColor )

   for x in range( x3, x4 ):
      for y in range( y3, y4 ):
         pixel = getPixel (image, x, y)
         red   = getRed(pixel)
         green = getGreen(pixel)
         blue  = getBlue(pixel)
         newColor = makeColor( 0, green, blue )
         setColor( pixel, newColor )
   return image

# this is how this function is called for the little boy:
image = fixRedEyes( image, 137,221,156,239,  247,235,262,255 )