As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.
$ cd ~/cs120/labs $ mkdir lab27 $ cd lab27
Write a function called average_file(opened_file)
,
which takes
as a parameter an opened file. The opened file is a file that
contains only
numbers, each on their own line. Your function should return
a floating
point number, which is the average of all of the numbers from
the file.
Write a function
called non_blank_lines(opened_file)
, which
takes an opened file as a parameter. Your function should
return a list of
lines from the file, which contains only lines that are not
blank.
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.
Create a python program in a file
called enlightening.py
. This program should read in
some 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!
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.
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.
Create a program called greyscale.py
, which converts
images to greyscale.
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 3 color poster. Here, we are going to only use 2 colors: black and white.
Create a python program in a file
called posterize.py
. This program should read in
some PPM image file, and write 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 three 0's to the file. Otherwise, write 3 255's.
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 the for line in
input_file
type of loop! The loop is already reading
one of the lines for you.
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!