Class 33 — Friday April 10
Chrestomatics and patterning
TBD
Look both ways
Zoom
Agenda
- Problem solving
- Image manipulation
- Admire past artistry
- Prepare for image manipulation
To do
- Consider practice modules
- Homework 28: image drawing
- Homework 29: problem solving
Downloads
- Practice module luna.py
- Practice module calc.py
- Practice module eval.py
- Example module uate.py
- Example module sigma.py
- Homework module trio.py
- Homework module flat.py
Practice module luna.py
- Defines a function
h()
. The function has a single numeric parameterx
. The function returns the number of hours it takes to get the moon while traveling at a speed ofx
miles per hour. For your information:
elapsed time = distance / speed
- For your convenience the module defines the constant
DISTANCE_IN_MILES_TO_MOON = 238900.0
- Observation: the function makes a straight-forward calculation and returns the result.
- The output of its built-in testing should be
h( 119.45 ) = 2000.0
h( 597.25 ) = 400.0
Practice module calc.py
- Defines a function
e()
. The function has three parametersx
,y
, ands
. Parametersx
andy
are decimals; parameters
is a string. Ifs
is either'+'
,'-'
,'*'
, or'/
', the function returns respectivelyx + y
,x - y
,x * y
, orx / y
. Otherwise, the functionsNone
.
- Observation: an
if-elif-...else
structure is needed to determine which of five possibile cases applies based on parameters
. Knowing which case applies, allows a straight-forward calculation and then the immediate return of the calculation
- The output of its built-in testing should be
19.5 + 5.25 = 24.75
12.5 - 6.5 = 6.0
12.5 * 4.5 = 56.25
10.0 / 2.25 = 4.444444444444445
1.0 @ 5.0 = None
Practice module eval.py
- Defines a functions
f()
. The function has two list parametersx
andy
. The function returns a new list whose elements are the elements ofx
followed by the elements ofy
. The function does not change its list parameters.
- The built-in testing makes use of the following lists.
x1 = [ ]; y1 = [ ]
x2 = [ 3, 1, 4 ]; y2 = [ ]
x3 = [ ]; y3 = [2, 7, 8]
x4 = [ 3, 1, 4 ]; y4 = [1, 5, 1, 9]
- The output of its built-in testing should be
f( x1, y1 ) = [ ]
f( x2, y2 ) = [ 3, 1, 4 ]
f( x3, y3 ) = [ 2, 7, 8 ]
f( x4, y4 ) = [ 3, 1, 4, 1, 5, 1, 9 ]
Example module uate.py
- Defines a function
g()
. The function has one list parameterx
. The function returns a new list whose elements are the element values ofx
without duplication. The function does not change its list parameter.
- Observation: an accumulator is needed. The elements of
x
need to be added to the accumulator one-at-a-time if they are not already there. Afterwards, the accumulator is returned.
- The built-in testing makes use of the following lists.
x1 = [ 0, 1, 2 ]
x2 = [ 0, 4, 1, 2, 2, 1, 3, 6, 3, 3, 4 ]
x3 = [ ]
- The output of its built-in testing should be
g( x1 ) = [ 0, 1, 2 ]
g( x2 ) = [ 0, 4, 1, 2, 3, 6 ]
g( x3 ) = [ ]
Example module sigma.py
- Defines a function
s()
. The function has one parameterd
. Parameterd
is an already initialized integer dataset; that is, it is a list of integer lists. The function returns the sum of the dataset values. The function does not change its list parameter.
- Observation: an accumulator is needed to store the sum of the dataset values. The sum of the values for each row of the dataset needs to be added to the accumulator. Afterwards, the accumulator is returned.
- The built-in testing makes use of the following lists.
d1 = [ [ 0 ], [ 1, 2 ], [ 1, 2, 3 ], [ 0 ] ]
d2 = [ [ 1, 0, 1, 2, 2 ], [ 3, 0, 1, 1, 1, 0 ], [ 2 ], [ 0, 0, 1 ] ]
d3 = [ [ 3, 0, 3], [ 3, 0, 3, 0, 1], [ 1, 0, 2 ] ]
d4 = [ ]
- The output of its built-in testing should be
s( d1 ) = 9
s( d2 ) = 15
s( d3 ) = 16
s( d4 ) = 0
Homework module trio.py
- Defines a function
t()
. The function has one list parameterx
of numeric values. The function does not change its list parameter. The function returns a new three-element list whose values are respectively the number of negative, zero, and positive values inx
.
- Observation: three accumulators are needed to store the three wanted counts. Each element of the list needs to be examined to determine which count should be incremented based on its numeric property. Afterwards, the return is a list of three elements (i.e, the computed counts).
- The built-in testing makes use of the following lists.
x1 = [ 0, -3, 0, -4, -2 ]
x2 = [ -3, 1, -2, 1, -3, -3, -2, -4, -1, -4 ]
x3 = [ 2, -1, 0, 3, 0, 3, -2, -2, -1, -4, 3, -4, 3, -1, 3 ]
x4 = [ ]
- The output of its built-in testing should be
t( x1 ) = [ 3, 2, 0 ]
t( x2 ) = [ 8, 0, 2 ]
t( x3 ) = [ 7, 2, 6 ]
t( x4 ) = [ 0, 0, 0 ]
Homework module flat.py
- Defines a function n(). The function has a single dataset parameter
d
.
- The function returns a simple list whose elements are the elements of first row of dataset
d
, followed by the elements of the second row of the dataset, followed by the elements of the third row of the dataset, and so on.
- The built-in tester makes use of the following definitions.
d1 = [ [ 0 ], [ 1, 2 ], [ 1, 2, 3 ], [ 0 ] ]
d2 = [ [ 1, 0, 1, 2, 2 ], [ 3, 0, 1, 1, 1, 0 ], [ 2 ], [ 0, 0, 1 ] ]
d3 = [ [ 3, 0, 3], [ 3, 0, 3, 0, 1], [ 1, 0, 2 ] ]
d4 = [ ]
- The output of its built-in testing should be
n( d1 ): [0, 1, 2, 1, 2, 3, 0]
n( d2 ): [1, 0, 1, 2, 2, 3, 0, 1, 1, 1, 0, 2, 0, 0, 1]
n( d3 ): [3, 0, 3, 3, 0, 3, 0, 1, 1, 0, 2]
n( d4 ): []
Some possible image transformations
Duplicated image
Mirrored image
Flipped image
Clockwise rotation
Basic photo-manipulation problem-solving pattern
# get dimensions of the original
ow, oh = ...
# set dimensions of the new image
nw, nh = ...
# get a new appropriately sized image
new_image = Image.new( 'RGB', ( nw, nh ) )
# fill in every pixel of the new image
for nx in range( 0, nw ) : # consider every x value for the new image
for ny in range( 0, nh ) : # in tandem with every y value for the image
# set the spot to be filled in the new image
nspot = (nx ,ny )
# determine the corresponding spot of interest in the original
ospot = ...
# get the pixel at the ospot
opixel = ...
# determine the pixel for the new image
npixel = ...
# set the nspot in the new image
...
# return the filled in new image
return new_image
Basic photo-manipulation problem-solving Python function
def manip( original, size, color, where ) :
''' Provide a pattern for image manipulation
'''
# set dimensions of the new image
nw, nh = size( original )
# get a new appropriately sized image
new_image = Image.new( 'RGB', ( nw, nh ) )
# fill in every pixel of the new image
for nx in range( 0, nw ) : # consider every x value for the new image
for ny in range( 0, nh ) : # in tandem with every y value for the image
# set the spot to be filled in the new image
nspot = ( nx, ny )
# determine the corresponding spot of interest in the original
ospot = where( nspot, original )
# get the pixel at the ospot
opixel = original.getpixel( ospot )
# determine the pixel for the new image
npixel = color( opixel )
# set the nspot in the new image
new_image.putpixel( nspot, npixel )
# return the filled in new image
return new_image
© 2020 Jim Cohoon | Resources from previous semesters are available. |