Unformatted text preview: 11/21/2008 Assignment ECS 10
11/21 Your Your program will take an image file, and do two effects: color color to black and white pixelization pixe You You will do this using functions. One One takes a color and converts it to a shade of gray. One One colors a square of pixels a single color. You You may work in pairs. Functions
def isFloat(s): try: float(s) except: return False return True Structure of Function
Defined Defined starting with def def Parameters Parameters inside parenthesis. These variables hold the input data inside the function. Output returned by the return command. return comman Using Using the return command leaves the return function. parameter (input) return value (output) From the outside…
Functions Functions are “called on” arguments. The arguments don’t change. After this: reply = raw_input(‘Enter interest rate: ‘) rate = float(reply) reply reply is still a string; the function float() did float() nothing to it. rate rate is a float (if there was no error). Functions Functions in Program
You You can define functions in the same file as the program that uses them, or in a module. Put Put function definitions at the top of the file, right after the imports. Functions must be right Functions defined defined before they can be used! The The code in a function is not run until the function is called, whether it is in a separate module or in the file with the program. 1 11/21/2008 On the inside…
What What happens in functions, stays in functions. functions. Changing Changing parameters does not change arguments def add_one(x): dd x = x+1 return x age = 5 next = add_one(age) print "I am",age,"and soon I will be",next Color
Color Color is a tuple (Red, Green, Blue) (255, 0, 0) is bright red (0, 0, 0) is black (255, 255, 255) is…? (50, 0, 0) is dark red What would pink be? Pastel Pastel Function
Make Make all colors delicate pastels. Means Means mixing some white into them. First First get image data. Write Write function to change colors. Put Put new colors in image pixels. Image Image module
size size – Variable in the module, a tuple (w,h) w,h) containing width and height of image. getpixel( (x,y)) getpixel( (x,y))– Argument is a tuple, (x,y), a tuple, (x,y), position position in the image. The method returns The the the color at that pixel. (0,0) (1,0) (2,0) (0,1) (1,1) (2,1) … … … Image module
putpixel( (x,y), (r,g,b)) putpixel( (x,y), (r,g,b)) – Arguments are two tuples. (x,y) is a position in the image. tuples. (x,y) (r,g,b) is a color. Puts the color at the r,g,b) position position. (0,0) (1,0) (2,0) (0,1) (1,1) (2,1) … … … 2 ...
View Full Document
This note was uploaded on 03/23/2011 for the course ECS 10 taught by Professor Amenta during the Winter '07 term at UC Davis.
- Winter '07