Difference between revisions of "CSC111 Homework 5 Solution 2011"

From dftwiki3
Jump to: navigation, search
(Solution Program)
 
Line 10: Line 10:
 
=Solution Program=
 
=Solution Program=
  
*TBA
 
  
 +
<source lang="python">
 +
#hw5.py
 +
#111a-ar
 +
#Gavi Levy Haskell
 +
#
 +
#Takes the data from userInfo.py, inverts and then scram-
 +
#bles the color values of the image, gives the picture a
 +
#scaly texture using a pixelation function based on the
 +
#Pixelating Clooney program, adds a border in three colors
 +
#picked randomly from the processed image, and adds the
 +
#caption specified in userInfo.py.
 +
#
 +
#
 +
#Doesn't work well with very small images (75x75 is probably
 +
#the minimum), and very long captions with small images won't
 +
#work either.
 +
#
 +
#
 +
#NOTE: 5 or 6 added to some values in order to compensate
 +
#for display problem in graphics window
 +
 +
from userInfo import *
 +
from graphics import *
 +
from random import randrange
 +
 +
#inverts and scrambles the colors of the image
 +
def color(pbar, progbar, img):
 +
    for x in range(width):
 +
        for y in range(height):
 +
            r,g,b = img.getPixel(x,y)
 +
            r = 255 - r  #inverts r
 +
            g = 255 - g  #inverts g
 +
            b = 255 - b  #inverts b
 +
            img.setPixel(x,y,
 +
                        color_rgb(g, b, r))#swaps colors
 +
        if x%10 == 0:
 +
            pbar = pbar + "-"
 +
            progbar.setText(pbar)
 +
 +
 +
 +
 +
#pixelates the image--adapted from Pixelating Clooney program
 +
def pixel(win, img, w, h):
 +
    px  = 5 #sets circle size (radius) 
 +
 +
    #loop to get, create each pixel
 +
    for x in range(0, width, px*2):
 +
       
 +
        #allows user to abort this filter
 +
        if win.checkMouse() != None:
 +
            img.draw(win) #prints image if filter is aborted
 +
            break
 +
       
 +
        for y in range(0, height, px*2):
 +
            #checks if pixel is within image
 +
            #(pxw and pxy set location to get pixel from)
 +
            if x + px < width:
 +
                pxw = x + px
 +
            else:
 +
                pxw = width - 1
 +
 +
            if y + px < height:
 +
                pxy = y + px   
 +
            else:
 +
                pxy = height - 1
 +
 +
               
 +
            #gets color of the pixel
 +
            R,G,B = img.getPixel(pxw, pxy)
 +
           
 +
            #defines new pixel (+23 adjusts for border width)
 +
            pixel = Circle(Point(x + px + 23, y + px + 23),
 +
                          1.5*px) #larger size=scale effect
 +
            pixel.setWidth(0)
 +
            pixel.setFill(color_rgb(R, G, B))
 +
            pixel.draw(win)
 +
       
 +
 +
 +
 +
 +
#draws borders on image based on colors selected from it
 +
def border(win, img, w, h):
 +
    #chooses colors from random points in image
 +
    w1 = randrange(width)
 +
    h1 = randrange(height)
 +
    w2 = randrange(width)
 +
    h2 = randrange(height)
 +
    w3 = randrange(width)
 +
    h3 = randrange(height)
 +
   
 +
    #borders
 +
    R1, G1, B1 = img.getPixel(w1, h1)  #color from image
 +
    bd1 = Rectangle(Point(21, 21), Point(w - 14, h - 14))
 +
    bd1.setWidth(8)
 +
    bd1.setOutline(color_rgb(R1, G1, B1)) #sets border color
 +
    bd1.draw(win)                       
 +
 +
    R2, G2, B2 = img.getPixel(w2, h2)
 +
    bd2 = Rectangle(Point(15, 15), Point(w - 8, h - 8))
 +
    bd2.setWidth(4)
 +
    bd2.setOutline(color_rgb(R2, G2, B2)) #sets border color
 +
    bd2.draw(win)                       
 +
 +
    R3, G3, B3 = img.getPixel(w3, h3)
 +
    bd3 = Rectangle(Point(9, 9), Point(w - 2, h - 2))
 +
    bd3.setWidth(8)
 +
    bd3.setOutline(color_rgb(R3, G3, B3)) #sets border color
 +
    bd3.draw(win)                       
 +
 +
 +
 +
 +
#creates plaque and caption
 +
def plaque(win, w, h):
 +
    #caption plaque--so caption shows on any color
 +
    pwi = len(caption)*10 + 4
 +
    cbg = Rectangle(Point(w//2 - pwi//2, h + 2),
 +
                    Point(w//2 + pwi//2, h - 16))
 +
    cbg.setWidth(2)
 +
    cbg.setFill("white")
 +
    cbg.draw(win)
 +
   
 +
    #caption
 +
    cap = Text(Point(w//2, h - 7), caption.title())
 +
    cap.setSize(16)
 +
    cap.setFace("times roman")
 +
    cap.setStyle("bold")
 +
    cap.draw(win)
 +
 +
 +
 +
#main function (sets up variables, calls functions)
 +
def main():
 +
    #adds space to width and height for border
 +
    w  = width  + 40
 +
    h  = height + 40
 +
   
 +
    #sets up window
 +
    win = GraphWin("111a-ar: Image Filter", w, h)
 +
    win.setBackground("white")
 +
 +
    #"loading" message, progress bar, abort message
 +
    loading = Text(Point(w//2, h//2), "LOADING")
 +
    loading.draw(win)
 +
    #prints box around progress bar
 +
    pbw = ((width//10)*2)//2 + 2
 +
    progbox = Rectangle(Point(w//2 + pbw, h//2 + 17),
 +
                        Point(w//2 - pbw, h//2 + 12))
 +
    progbox.setFill("black")
 +
    progbox.draw(win)
 +
    pbar    = "" #variable for progress bar
 +
    progbar = Text(Point(w//2, h//2 + 15), pbar)
 +
    progbar.setFill("green")
 +
    progbar.setSize(7)
 +
    progbar.draw(win)
 +
    stop    = Text(Point(w//2, h//2),
 +
                  "click to abort pixelation")
 +
 +
    #original image, centered
 +
    img = Image(Point((w + 6)//2, (h + 6)//2), filename)
 +
 +
    #calls filter functions
 +
    color(pbar, progbar, img)
 +
    loading.undraw() #undraws loading message and
 +
    progbar.undraw() #progress bar
 +
    progbox.undraw()
 +
   
 +
    stop.draw(win) #prints pixel filter message
 +
    pixel(win, img, w, h)
 +
    stop.undraw() #undraws pixel filter message
 +
   
 +
    border(win, img, w, h)
 +
   
 +
    plaque(win, w, h)
 +
   
 +
 +
    #closes window
 +
    win.getMouse()
 +
    win.close()
 +
 +
 +
 +
main()
 +
</source>
  
 
<br />
 
<br />

Latest revision as of 19:57, 1 November 2011

--D. Thiebaut 09:23, 27 October 2011 (EDT)


A Sample of Portraits


CSC111 Collage 2011.png


Solution Program

#hw5.py
#111a-ar
#Gavi Levy Haskell
#
#Takes the data from userInfo.py, inverts and then scram-
#bles the color values of the image, gives the picture a
#scaly texture using a pixelation function based on the
#Pixelating Clooney program, adds a border in three colors
#picked randomly from the processed image, and adds the
#caption specified in userInfo.py.
#
#
#Doesn't work well with very small images (75x75 is probably
#the minimum), and very long captions with small images won't
#work either.
#
#
#NOTE: 5 or 6 added to some values in order to compensate
#for display problem in graphics window
 
from userInfo import *
from graphics import *
from random import randrange

#inverts and scrambles the colors of the image
def color(pbar, progbar, img):
    for x in range(width):
        for y in range(height):
            r,g,b = img.getPixel(x,y)
            r = 255 - r  #inverts r
            g = 255 - g  #inverts g
            b = 255 - b  #inverts b
            img.setPixel(x,y,
                         color_rgb(g, b, r))#swaps colors
        if x%10 == 0:
            pbar = pbar + "-"
            progbar.setText(pbar)




#pixelates the image--adapted from Pixelating Clooney program
def pixel(win, img, w, h):
    px  = 5 #sets circle size (radius)   

    #loop to get, create each pixel
    for x in range(0, width, px*2):
        
        #allows user to abort this filter
        if win.checkMouse() != None:
            img.draw(win) #prints image if filter is aborted
            break
        
        for y in range(0, height, px*2):
            #checks if pixel is within image
            #(pxw and pxy set location to get pixel from)
            if x + px < width:
                pxw = x + px
            else:
                pxw = width - 1

            if y + px < height: 
                pxy = y + px    
            else:
                pxy = height - 1

                
            #gets color of the pixel
            R,G,B = img.getPixel(pxw, pxy)
            
            #defines new pixel (+23 adjusts for border width)
            pixel = Circle(Point(x + px + 23, y + px + 23),
                           1.5*px) #larger size=scale effect
            pixel.setWidth(0)
            pixel.setFill(color_rgb(R, G, B))
            pixel.draw(win)
        




#draws borders on image based on colors selected from it
def border(win, img, w, h):
    #chooses colors from random points in image
    w1 = randrange(width)
    h1 = randrange(height)
    w2 = randrange(width)
    h2 = randrange(height)
    w3 = randrange(width)
    h3 = randrange(height)
    
    #borders
    R1, G1, B1 = img.getPixel(w1, h1)  #color from image
    bd1 = Rectangle(Point(21, 21), Point(w - 14, h - 14))
    bd1.setWidth(8)
    bd1.setOutline(color_rgb(R1, G1, B1)) #sets border color
    bd1.draw(win)                         

    R2, G2, B2 = img.getPixel(w2, h2)
    bd2 = Rectangle(Point(15, 15), Point(w - 8, h - 8))
    bd2.setWidth(4)
    bd2.setOutline(color_rgb(R2, G2, B2)) #sets border color
    bd2.draw(win)                         

    R3, G3, B3 = img.getPixel(w3, h3)
    bd3 = Rectangle(Point(9, 9), Point(w - 2, h - 2))
    bd3.setWidth(8)
    bd3.setOutline(color_rgb(R3, G3, B3)) #sets border color
    bd3.draw(win)                         




#creates plaque and caption
def plaque(win, w, h):
    #caption plaque--so caption shows on any color
    pwi = len(caption)*10 + 4
    cbg = Rectangle(Point(w//2 - pwi//2, h + 2),
                    Point(w//2 + pwi//2, h - 16))
    cbg.setWidth(2)
    cbg.setFill("white")
    cbg.draw(win)
    
    #caption
    cap = Text(Point(w//2, h - 7), caption.title())
    cap.setSize(16)
    cap.setFace("times roman")
    cap.setStyle("bold")
    cap.draw(win)



#main function (sets up variables, calls functions)
def main():
    #adds space to width and height for border
    w   = width  + 40
    h   = height + 40
    
    #sets up window
    win = GraphWin("111a-ar: Image Filter", w, h)
    win.setBackground("white")

    #"loading" message, progress bar, abort message
    loading = Text(Point(w//2, h//2), "LOADING")
    loading.draw(win)
    #prints box around progress bar
    pbw = ((width//10)*2)//2 + 2
    progbox = Rectangle(Point(w//2 + pbw, h//2 + 17),
                        Point(w//2 - pbw, h//2 + 12))
    progbox.setFill("black")
    progbox.draw(win)
    pbar    = "" #variable for progress bar
    progbar = Text(Point(w//2, h//2 + 15), pbar)
    progbar.setFill("green")
    progbar.setSize(7)
    progbar.draw(win)
    stop    = Text(Point(w//2, h//2),
                   "click to abort pixelation")

    #original image, centered
    img = Image(Point((w + 6)//2, (h + 6)//2), filename)

    #calls filter functions
    color(pbar, progbar, img)
    loading.undraw() #undraws loading message and
    progbar.undraw() #progress bar
    progbox.undraw()
    
    stop.draw(win) #prints pixel filter message
    pixel(win, img, w, h)
    stop.undraw() #undraws pixel filter message
    
    border(win, img, w, h)
    
    plaque(win, w, h)
    

    #closes window
    win.getMouse()
    win.close()


 
main()