Homework 26 – image manipulation

Due Friday, April 20


Module do.py

sx, sy = spot

sr, sg, sb = drawing.getpixel( spot )




Function random_color()

random.seed( 'cs' )

c1 = do.random_color()

random.seed( '1112' )

c2 = do.random_color()

print( c1, c2 )

should print

(104, 190, 51) (190, 230, 218)




Function get_width( drawing )

ml_url = 'http://www.cs1112.org/images/ml.png'

tj_url = 'http://www.cs1112.org/images/tj.png'

ml = url.get_image( ml_url )

tj = url.get_image( tj_url )

w1 = do.get_width( ml )

w2 = do.get_width( tj )

print( w1, w2 )

should print

512 580




Function get_height( drawing )

ml_url = 'http://www.cs1112.org/images/ml.png'

tj_url = 'http://www.cs1112.org/images/tj.png'

ml = url.get_image( ml_url )

tj = url.get_image( tj_url )

h1 = do.get_height( ml )

h2 = do.get_height( tj )

print( h1, h2 )

should print

765 664




Function left( spot )

xy1 = ( 2, 4 );

xy2 = ( 3, 9 )

l1 = do.left( xy1 )

l2 = do.left( xy2 )

print( l1, l2 )

should print

(1, 4) (2, 9)




Function right( spot )

xy1 = ( 2, 4 );

xy2 = ( 3, 9 )

r1 = do.right( xy1 );

r2 = do.right( xy2 );

print( r1, r2 )

should print

(3, 4) (4, 9)




Function above( spot )

Returns the coordinate immediately above spot; that is, its x-coordinate is the same as that of spot and its y-coordinate is one less than that of spot. For example, code segment

xy1 = ( 2, 4 );

xy2 = ( 3, 9 );

a1 = do.above( xy1 );

a2 = do.above( xy2 );

print( a1, a2 )

should print

(2, 3) (3, 8)




Function below( spot )

xy1 = ( 2, 4 );

xy2 = ( 3, 9 );

b1 = do.below( xy1 );

b2 = do.below( xy2 );

print( b1, b2 )

should print

(2, 5) (3, 10 )




Function is_inbounds( drawing, spot )

For example, code segment

ml_url = 'http://www.cs1112.org/images/ml.png'

ml = url.get_image( ml_url )

w = do.get_width( ml )

h = do.get_height( ml )

xy1 = ( w // 2, h // 2 )

xy2 = ( w, h )

xy3 = ( -1, -1 )

i1 = do.is_inbounds( ml, xy1 )

i2 = do.is_inbounds( ml, xy2 )

i3 = do.is_inbounds( ml, xy3 )

print( i1, i2, i3 )

should print

True False False




Function get_color( drawing, spot )

ml_url = 'http://www.cs1112.org/images/ml.png'

ml = url.get_image( ml_url )

w = do.get_width( ml )

h = do.get_height( ml )

xy1 = ( w // 4, h // 5 )

xy2 = ( w, h )

xy3 = ( -1, -1 )

c1 = do.get_color( ml, xy1 )

c2 = do.get_color( ml, xy2 )

c3 = do.get_color( ml, xy3 )

print( c1, c2, c3 )

should print

(137, 128, 97) None None




Function is_colorable( drawing, spot, bg=(255,255,255) )

ml_url = 'http://www.cs1112.org/images/ml.png'

ml = url.get_image( ml_url )

w = do.get_width( ml )

h = do.get_height( ml )

xy1 = ( w // 5, h // 4 )

xy2 = ( w, h )

xy3 = ( -1, -1 )

xy4 = ( w // 4, h // 5 )

b1 = do.is_colorable( ml, xy1 )

b2 = do.is_colorable( ml, xy2, (255, 0, 255 ) )

b3 = do.is_colorable( ml, xy3 )

c = ml.getpixel( xy4 )

b4 = do.is_colorable( ml, xy4, c )

print( b1, b2, b3, b4 )

should print

False False False True




Function paint( drawing, spot, c, bg=(255,255,255) )

ml_url = 'http://www.cs1112.org/images/ml.png'

ml = get.web_image( ml_url )

for y in range( 175, 220 ) :

  for x in range( 228, 235 ) :

  xy = ( x, y )

  bg = ml.getpixel( xy )

  do.paint( ml, xy, ( 255, 215, 0 ), bg )

get.popup( ml )

should produce

marred mona lisa




†. The above code segments are bundled into snippets.py.

 



Warning: fopen(/l/cs1112/public_html/defs/htm/181-trailer): Failed to open stream: No such file or directory in /l/cs1112/public_html/defs/php/read.php on line 5
Unable to open file!