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.
Details
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!
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.
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.
Details
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.
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!
Submission
Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.