In a file called functions.py, write the functions listed in the table below. Include a docstring comment for each function. The docstring should be an appropriate rephrasing of the specification on the right-hand side of the table. See the Assignment rules page for guidance on how to write good docstrings.

We give the parameter types for each function. When you write your function definitions, you will replace these with actual names for the parameters; make sure you pick good names.

Some of the functions are useful "helpers" for other functions. That is, they can be called by another function to do part of its work. Take advantage of this to avoid repetitive code. You are permitted to write additional helper functions as well.

Notice that some functions change a picture they are given, while others create an entirely new picture. Be very careful about this difference when writing your functions. If you make the right effect, but on the wrong picture, your function will be marked as incorrect.

Functions watermark and morph each produce a new picture based on two given pictures, but they do it in different ways. The picture produced by watermark looks like the first picture it is given, but with a faint impression of the second picture overlaid on it. You may have seen this effect with a word processor, e.g., when the word "draft" is written faintly in large letters across a document. (The term "watermark" comes from a design made in some high quality paper, and that can be seen when the paper is held up to the light.) The morph function mixes two given pictures equally, creating a new picture that looks like the first picture along the left-hand side, but slowly transitions into looking like the second picture as you go to the right.

Function Name Description
how_far(int, int, int) This function is given three ints. The first is less than or equal to the second, and the third int falls somewhere in the range of the other two, inclusive. Return a float between 0 and 1 that indicates how far along in this range the third int is.
Example: how_far(3, 19, 3) should return 0.0, how_far(3, 19, 19) should return 1.0, and how_far(3, 19, 7) should return 0.25

smallest_dimension(Picture, Picture) Given a two Pictures, return an int which is the smallest of their four dimensions. In other words, return either the width or height of one of the Pictures, whichever is smallest. For full marks your function must not us an if-statement.

blend(Pixel, Pixel, float) Correction:
Given two Pixels and a float between 0 and 1 inclusive, return a new Color that is a blend of colors of the two Pixels. The red component of the new Color should be composed of f times the red from the first Pixel and (1-f) times the red from the second Pixel, where f is the float that is passed in. The green and blue components should be created in the same way.

amount_color(Pixel) Given a Pixel, return the amount of color (as an int) in that Pixel, i.e., the sum of its red, green, and blue components.

total_color(Picture) Given a Picture, return the total amount of color (as an int) in all the pixels in that Picture.

more_color(Picture, Picture) Return True if the first Picture has more color in total than the second; and False otherwise. For full marks your function must not use an if-statement and its body must be one line long.

watermark(Picture, Picture) The two Picture parameters have exactly the same dimensions. Return a new Picture with a watermark effect: Each pixel in it should be a blend that is 80% the color of the corresponding pixel from the first picture, and 20% the color of the corresponding pixel from the second picture.

adjust_color(Picture, float) Multiply the red, green and blue components of each pixel in the Picture by the float. The float is between 0 and 1 inclusive.

caption(Picture, string) Place the given string on the given Picture as a caption, as follows: Place a white rectangle, 25 pixels high, on the bottom part of the Picture. It should have a 25-pixel margin on its left, right, and bottom sides. Put the string, in black, on the rectangle to form a caption on the Picture. The string should be 5 pixels down and 5 pixels to the right of the upper-left corner of the caption box. Hint: media functions add_rect_filled and add_text will be useful.

Note: If the string is too long, it will continue outside the caption box and eventually be cut off at the margin of the Picture. Just let that happen; you do not have to check for that situation or do anything special if it occurs.

morph(Picture, Picture) The two Picture parameters have exactly the same dimensions. Return a new Picture in which the first Picture morphs from left to right into the second Picture, as follows: Pixels in the left-most column of the new Picture are 100% composed of pixels from the first Picture. Pixels in the right-most column are 100% composed of pixels from the second Picture. Each pixel in between is a blend that is determined by how far the pixel is between the left and right edge of the new Picture. For example, in a picture of width 1000 pixels, a pixel with x-value 300 is 30% of the way from the left to the right edge of the picture. Its color is therefore a blend that is 70% the color of the corresponding pixel from the first picture, and 30% the color of the corresponding pixel from the second picture.
More requirements

1. Your functions must have absolutely no user-input -- nothing that the user types, and no choosing of filenames with media.choose_file, for example. And your functions must have absolutely no output -- no messages to the user (not even a "welcome!") and no displaying of pictures.
2. Your module must consist of the following, in order:
* import media
* The definitions of your functions. (Order of the function definitions does not matter.)
* Optionally, you might have a "main" block. It must begin with if __name__ == "__main__": and the code that follows must be indented underneath it. This would be a suitable place to put code that tests your functions.
3. Functions blend and adjust_color compute red, green and blue values in a way that will yield a float result. Yet the red, green and blue values of a pixel must be of type int. Convert each of these floats into an int using the int function. This will discard any remainder. Do not try to do something better like rounding the values; we're not ready to get into some of the issues that rounding brings up. And more importantly, if you try to round, your code will give unexpected results and will be marked as incorrect.

Your functions can be written using only these functions from the media module:

* get_width and get_height
* create_picture
* get_pixel
* get_x, get_y
* get_green, get_blue and get_red
* set_green, set_blue, and set_red
* create_color
* get_color and set_color

You are welcome to use other functions from media, but having this list will help you focus on the most relevant ones.

## All 6 Replies

My advice, start writing each function using test parameters and temporary test prints. See if you get the expected results. If you have problems give us the code you have and we will try to help.

I need help with this assignment as well....for def total_color(pic), this is what I have so far:

def total_color(pic):
for pix in pic:
total_green = media.get_green(pix)
total_blue = media.get_blue(pix)
total_red = media.get_red(pix)
total = int(green) + int(blue) + int(red)

----

Does that make sense? Have I gone about it the right way? Thanks in advance for any feedback.

Absolutely bloody ridiculous. I'm in your class (CSC108) and I know the prof has offered office hours to help people with the assignment. There's no excuse for you to just paste the whole assignment into an online forum expecting someone to do it for you while other students are sitting here busting their balls trying to get it done on their own.

Nice work ethic, although I suppose it's my fault for expecting more from UofT students.

Absolutely bloody ridiculous. I'm in your class (CSC108) and I know the prof has offered office hours to help people with the assignment. There's no excuse for you to just paste the whole assignment into an online forum expecting someone to do it for you while other students are sitting here busting their balls trying to get it done on their own.

Nice work ethic, although I suppose it's my fault for expecting more from UofT students.

Laziness is virtue ONLY if you are lazy enough to be lazy person

Absolutely bloody ridiculous. I'm in your class (CSC108) and I know the prof has offered office hours to help people with the assignment. There's no excuse for you to just paste the whole assignment into an online forum expecting someone to do it for you while other students are sitting here busting their balls trying to get it done on their own.

Nice work ethic, although I suppose it's my fault for expecting more from UofT students.

I agree fully! Some of the forum members are teachers too. Helping some student is a fine line. We like to give the teacher a chance to help first, maybe this way some potential weaknesses in teaching the class can be unearthed. Also each class is slightly different at the level of understanding, and a good teacher adjust to that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.