Difference between revisions of "CSC111 Pixelating Clooney"
(Created page with "--~~~~ ---- <source lang="python"> """ pixelate.py D. Thiebaut This program takes a gif file (doesn't work with other file formats), and overlaps several small circles or rectan...") |
(→Pixelated with Circles) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] 13:22, 7 October 2011 (EDT) | --[[User:Thiebaut|D. Thiebaut]] 13:22, 7 October 2011 (EDT) | ||
---- | ---- | ||
+ | __TOC__ | ||
+ | |||
+ | <br /> | ||
+ | ==Program== | ||
<source lang="python"> | <source lang="python"> | ||
""" | """ | ||
Line 69: | Line 73: | ||
<br /> | <br /> | ||
+ | ==Example== | ||
+ | ===Unretouched Photo (gif format)=== | ||
+ | <br /> | ||
+ | <center> | ||
+ | [[Image:Clooney.gif]] | ||
+ | </center> | ||
+ | <br /> | ||
+ | ===Pixelated with Circles=== | ||
<br /> | <br /> | ||
+ | <center> | ||
+ | [[Image:ClooneyPixelated.png]] | ||
+ | </center> | ||
+ | <br /> | ||
+ | ===Pixelated with Squares=== | ||
+ | <br /> | ||
+ | <center> | ||
+ | [[Image:ClooneyPixelatedSquares.png]] | ||
+ | </center> | ||
<br /> | <br /> | ||
Latest revision as of 13:16, 7 October 2011
--D. Thiebaut 13:22, 7 October 2011 (EDT)
Contents
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)
Pixelated with Circles
Pixelated with Squares