Average File
Write the function average_file(filename)
that uses
file input to compute an average. The
parameter filename is the name of a text file as a
string. The file contains only numbers, each on their own line.
The function should return the average of all of the numbers in
the file filename as a float.
Example
The program:
print(average_file('in.txt'))
And the file in.txt:
-11 -23 -32 -44
Will produce the output:
-27.5
Write Dictionary
Write the function write_dictionary(dictionary,
filename)
that writes a dictionary to a file. The
parameter dictionary is a dictionary of any size that
has string keys and integer values. The
parameter filename is the name of the text file to
create as a string. The function should not return
anything. Instead, the function should create the
file filename with the contents of dictionary.
Each line of the file should contain an entry of the dictionary
where the key and value are separated by by a colon. The lines
of the file can be in any order.
Example
The program:
write_dictionary({'hey': 1, 'hello': 4, 'hi', 2}, 'out.txt')
Will produce the file:
hey:1 hello:4 hi:2
Lighten
PPM image files are fairly easy to read and write. Each file contains a header (which encodes information about the version of PPM, the size of the image, etc.), and then a series of integers representing color values on their own line. Making modifications of an existing PPM image requires modifying these color values. Simple modifications only require working with an single color value, such as lightening an image.
Details
Create a Python that reads a PPM image file, and write a different PPM image file which is the lightened version of the read file.
In order to lighten an image, you need to take a color value, and multiply it by 1.5. This is the color value that should be written to the new file. Make sure it's an integer in the range 0 - 255!
Example
Before

After

Hint
-
A PPM image has 3 lines at the beginning which encode meta data for the image. These 3 lines should be copied verbatim into the next image.
-
Recall that you can use the
int
function to convert a floating point value into an integer. -
You will get errors in your image if you write a value greater than 255 in the new image file. If you need to write a value greater than 255, simply write 255 into the image file.
Challenge
Another common filter people apply to images is a greyscale filter. This filter goes through the images, and converts every pixel into a grey color. A grey pixel is simply a pixel whose 3 color values are exactly the same. A simple way to accomplish this is to compute the average of the 3 color values for a pixel, and write this as the RGB value of each one.
Posterize
While you might not recognize the name "posterize," I'm certain you have seen an example of it. Posterization is a method by which the number of colors in the image is reduced. In the case of the most famous example, it was reduced to a 4 color poster. Here, we are going to only use 2 colors: black and white.
Details
Create a Python that reads in a PPM image file, and writes a different PPM image file which is the posterized version of the read file.
To posterize a picture, you must read 3 color values at a time from the file. Read these 3 colors values and compute the average of them. If the value is less than 128, then you should write white, three 0's, to the file. Otherwise, write black, three 255's, to the file.
Example
Before

After

Hint
- A PPM image has 3 lines at the beginning which encode meta data for the image. These 3 lines should be copied verbatim into the next image.
-
Recall that you can use the
int
function to convert a floating point value into an integer. -
Be careful if you are mixing calls to
readline
while you are also using thefor line in input_file
type of loop! The loop is already reading one of the lines for you.
Challenge
Modify your program so that it does some other number of colors. In the popular Obama example, they used 4 colors: red, light blue, dark blue, and beige. For this, at least use 3 colors!