CSC111 Homework 7 Solution 2014
Revision as of 18:24, 3 April 2014 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- =Solution Program= <br /> <center> Image:hw7bm.png </center> <br /> <source lang="python"> #hw7a.py #Youyou Tian (bm) #3/27/14 #This program performs three tra...")
--D. Thiebaut (talk) 19:24, 3 April 2014 (EDT)
Solution Program
#hw7a.py
#Youyou Tian (bm)
#3/27/14
#This program performs three transformations on a given picture
#It copies the original picture on the upper left 3 times, so
#there are 4 copies of the picture arranged in a 2x2 "grid."
#A border is placed around the final image
#
#The upper right transformation turns the picture black white
#
#The lower left transformation creates alternating stripes with a
#negative inverted colors transformation
#
#The final corner, lower right, is split up into quadrants where
#two quareants are given a circular fog transformation that starts
#from the center of the picture and increases in fogginess the further
#the pixel is from the center
#-----------------------------------------------------------------
#Transforms the pixel to greyscale, b/w
def greyScale(pixel):
red = getRed(pixel)
green = getGreen(pixel)
blue = getBlue(pixel)
grey = int(.3 * red + .6 * green + .11 * blue)
return makeColor(grey, grey, grey)
#-----------------------------------------------------------------
#Does the upper right copy greyscale transformation
def upperRight(image, xMin, yMax):
for x in range(xMin, getWidth(image)):
for y in range(0, yMax):
pixel = getPixel(image, x-xMin, y)
bwPixel = greyScale(pixel)
pixelReplace = getPixel(image, x, y)
setColor(pixelReplace, bwPixel)
return image
#-----------------------------------------------------------------
#Checks the height position of the pixel which controls
#the width of the stripes. Here, for every 20 pixels
#the boolean switch would change from true to false
#or vice versa and controls the main transformation function
def checkStripeSwitch(switch, y):
if y % 20 == 0:
switch = not switch
return switch
#-----------------------------------------------------------------
#Takes a pixel and inverts its colors
def invert(pixel):
invertRed = 255 - getRed(pixel)
invertGreen = 255 - getGreen(pixel)
invertBlue = 255 - getBlue(pixel)
return makeColor(invertRed, invertGreen, invertBlue)
#-----------------------------------------------------------------
#Does the lower left transformation. The type of stripe that
#switches is controlled by a boolean variable
def lowerLeft(image, xMax, yMin):
switch = True
for x in range(0, xMax):
for y in range(yMin, getHeight(image)):
switch = checkStripeSwitch(switch, y)
pixel = getPixel(image, x, y-yMin)
if switch:
pixel = invert(pixel)
else:
pixel = getColor(pixel)
pixelReplace = getPixel(image, x, y)
setColor(pixelReplace, pixel)
return image
#-----------------------------------------------------------------
#distance formula, returns distance between two pixels
def dist(x1, y1, x2, y2):
return sqrt((x1-x2)**2 + (y1-y2)**2)
#-----------------------------------------------------------------
#depending on the distance the pixel is from the center
#adds a fog level the further it is from the center, the foggier
#and whiter it will be. Thus, creates a circular gradient pattern
def circleFog(pixel, x, y, xCenter, yCenter, xMaxDist, yMaxDist):
red = getRed(pixel)
green = getGreen(pixel)
blue = getBlue(pixel)
distance = dist(x,y,xCenter, yCenter)
maxDistance = dist(xCenter, yCenter, xMaxDist, yMaxDist)
fogFactor = 1.0 * distance/maxDistance
red = red + int((255 - red) * fogFactor)
green = green + int((255 - green) * fogFactor)
blue = blue + int((255 - blue) * fogFactor)
return makeColor(red, green, blue)
#-----------------------------------------------------------------
#Creates the lower right transformation of "circular"
#fog. If it is withing hte boundaries of the upper left and
#lower right of the copy, the transformation will be applied
def lowerRight(image, xMin, yMin):
xMiddle = xMin + ((getWidth(image) - xMin)//2)
yMiddle = yMin + ((getHeight(image) - yMin)//2)
for x in range(xMin, getWidth(image)):
for y in range(yMin, getHeight(image)):
pixel = getPixel(image, x - xMin, y - yMin)
pixelReplace = getPixel(image, x, y)
if (x<= xMiddle and y <= yMiddle)\
or (x > xMiddle and y > yMiddle):
pixel = circleFog(pixel, x, y, xMiddle, yMiddle, xMin, yMin)
else:
pixel = getColor(pixel)
setColor(pixelReplace, pixel)
return image
#-----------------------------------------------------------------
#Creates a cyan border of a given width around the entire image
def makeBorder(image, borderWidth):
borderWidthMax = getWidth(image) - 1 - borderWidth
borderWidthMin = borderWidth
borderHeightMax = getHeight(image) - 1 - borderWidth
borderHeightMin = borderWidth
color = makeColor(0, 255, 255)
for x in range(getWidth(image)):
for y in range(getHeight(image)):
if(x < borderWidthMin or x > borderWidthMax)\
or (y < borderHeightMin or y > borderHeightMax):
pixel = getPixel(image, x, y)
setColor(pixel, color)
return image
#-----------------------------------------------------------------
# Main Function, Creates the "Andy Warhol" like transformations
# for a given picture. abd puts a border around it
def main():
file = pickAFile()
image = makePicture( file )
xMiddle = getWidth(image)//2
yMiddle = getHeight(image)//2
image = upperRight(image, xMiddle, yMiddle)
image = lowerLeft(image, xMiddle, yMiddle)
image = lowerRight(image, yMiddle, yMiddle)
image = makeBorder(image, 10)
repaint(image)
main()