CSC111 Pixelating Clooney

From dftwiki3
Jump to: navigation, search

--D. Thiebaut 13:22, 7 October 2011 (EDT)



Program

"""
pixelate.py
D. Thiebaut

This program takes a gif file (doesn't work with other file formats), and
overlaps several small circles or rectangles over it, picking the color of the
whole circle or rectangle to be the color of its center when mapped to the
photograph.

This program crashes if the size of the square containing the rectangle or
the circle is not an exact divisor of the width or the length of the photo.

"""
from graphics import *

def main():
    # the width and length of the photograph
    w = 600
    h = 400
    win = GraphWin( "pixelate", w, h )

    # create an image from the photograph
    img = Image( Point( w//2, h//2 ), "clooney.gif" )
    img.draw( win )

    # pick the size of the squares/circles (sq=2 radii)
    sq = 10
    sq2 = sq//2

    # scan the image, one column at a time
    for x in range( 0, w, sq ):
        for y in range( 0, h, sq ):
            # pick the color of the pixel from the photo 
            # that is the middle of the current square or circle
            R,G,B = img.getPixel( x+sq2, y+sq2 )


            """
            # create a rectangle with side sq and color R, G, B
            r = Rectangle( Point( x, y ), Point( x+sq, y+sq ) )
            r.setWidth( 0 )            
            r.setFill( color_rgb( R, G, B ) )
            r.draw( win )
            """

            # create a circle with radius sq2, and color R, G, B
            # over a black rectangle with side sq2
            r = Rectangle( Point( x, y ), Point( x+sq, y+sq ) )
            r.setWidth( 0 )
            r.setFill( "black" )
            r.draw( win )
            c = Circle( Point( x+sq2, y+sq2 ), sq2 )
            c.setWidth( 0 )
            c.setFill( color_rgb( R, G, B ) )
            c.draw( win )

    # we're done converting the image
    Text( Point( 20, 20 ), "Click me!" ).draw( win )
    win.getMouse()
    win.close()

main()


Example

Unretouched Photo (gif format)


Clooney.gif


Pixelated with Circles


ClooneyPixelated.png


Pixelated with Squares


ClooneyPixelatedSquares.png