Class 33 — November 9
Live and learn
The greatest artists — Learned at the feet of masters — Copy and improve
Must come together — We are best when we are one — Everyone please
Look both ways
Agenda
- Continue to prepare for test 2.
- First class-ness
- Begin image manipulation examination
To do
- Review artifacts
- Check out imaging epistle
- Homework 30
Downloads
- Module di.py
- Module functions_are_first_class.py
- Program spooky.py
- Module manip.py
- Module copy.py
- Module blue.py
- Module reflect.py
Module di.py
- Defines function
ction()
with a list parameterx
. The function does not print anything. The function does not make any changes to its parameter.
- The function returns a dictionary. The keys to that dictionary are the elements of
x
. For each element inx
there is an entry in the dictionary that maps that element to the number of times it appears inx
.
- For example, if
x
equals['m',
'i',
'm',
'i',
'c']
, then the dictionary maps'c'
to1
,'i'
to2
, and'm'
to2
.
- The built-in tester for the module runs three tests using the following lists as arguments do
xtion()
.
x1 = ['m', 'i', 'm', 'i', 'c']
x2 = [3, 1, 2, 2, 1, 2]
x3 = [True, False, True, True]
- The tester should produce the following output.
ction( x1 ): {'m': 2, 'i': 2, 'c': 1}
ction( x2 ): {3: 1, 1: 2, 2: 3}
ction( x3 ): {True: 3, False: 1}
Functions as parameters
import functions_are_first_class
values = [ 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5 ]
a1 = functions_are_first_class.analyze( values, len )
print( "len(", values, "):", a1 )
a2 = functions_are_first_class.analyze( values, max )
print( "max(", values, "):", a2 )
a3 = functions_are_first_class.analyze( values, min )
print( "max(", values, "):", a3 )
a4 = functions_are_first_class.analyze( values, sum )
print( "sum(", values, "):", a4 )
Image manipulator
- Module manip.py
Some image manipulation descriptions
- Module copy.py
- Module blue.py
- Module reflect.py
Homework
- Module flip.py
I see you
| | Duplicate |
| | Bluing |
| | Reflection |
| | Flip |
| | Monotone |
| | Sepia |
| | Grayscale |
| | Clockwise rotation |
| | Palette reduction |
| | Zooming |
| | Pixelate |
Basic photo-manipulation problem-solving Python function
def manip( original, size, color, where ) :
''' Provide a pattern for image manipulation
function size(): determines size of new image based on the original
function color(): determines color of new image pixel based on an
original pixel
function where(): determines where on original to determine
correspondent pixel for new image
'''
# 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. |